Skip to content

Commit

Permalink
Merge branch 'main' into ci/helm-test
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-rm-meyer-ISST committed Jan 4, 2024
2 parents 2c34068 + 8c1ded5 commit e5e5193
Show file tree
Hide file tree
Showing 61 changed files with 3,846 additions and 517 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* Copyright (c) 2023 Volkswagen AG
* Copyright (c) 2023 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* Copyright (c) 2023, 2024 Volkswagen AG
* Copyright (c) 2023, 2024 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
* (represented by Fraunhofer ISST)
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -28,23 +28,23 @@
import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum;
import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum;
import org.eclipse.tractusx.puris.backend.common.api.logic.service.VariablesService;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner;
import org.eclipse.tractusx.puris.backend.masterdata.domain.model.*;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService;
import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService;
import org.eclipse.tractusx.puris.backend.stock.domain.model.*;
import org.eclipse.tractusx.puris.backend.stock.domain.model.measurement.MeasurementUnit;
import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ItemStockSammMapper;
import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.ItemUnitEnumeration;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.itemstocksamm.*;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.LocationIdTypeEnum;
import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto;
import org.eclipse.tractusx.puris.backend.stock.logic.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
Expand All @@ -71,7 +71,7 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner {
@Autowired
private MaterialItemStockService materialItemStockService;
@Autowired
private ReportedMaterialItemStockService reportedMaterialItemStockService;
private ItemStockSammMapper itemStockSammMapper;

@Autowired
private PartnerProductStockService partnerProductStockService;
Expand Down Expand Up @@ -151,6 +151,7 @@ private void createOwnPartnerEntity() {
private void setupCustomerRole() throws JsonProcessingException {
Partner supplierPartner = createAndGetSupplierPartner();
Material semiconductorMaterial = getNewSemiconductorMaterialForCustomer();
Partner mySelf = partnerService.getOwnPartnerEntity();

semiconductorMaterial = materialService.create(semiconductorMaterial);
log.info(String.format("Created material: %s", semiconductorMaterial));
Expand Down Expand Up @@ -197,13 +198,25 @@ private void setupCustomerRole() throws JsonProcessingException {
semiconductorMaterial,
5,
MeasurementUnit.piece,
"BPNS4444444444XX",
mySelf.getSites().first().getBpns(),
LocationIdTypeEnum.B_P_N_S,
new Date()
);
materialStockEntity = materialStockService.create(materialStockEntity);
log.info(String.format("Created materialStock: %s", materialStockEntity));

// Create Product Stock
ProductStock productStockEntity = new ProductStock(
centralControlUnitEntity,
20,
MeasurementUnit.piece,
mySelf.getSites().first().getBpns(),
LocationIdTypeEnum.B_P_N_S,
new Date(),
nonScenarioCustomer
);
productStockEntity = productStockService.create(productStockEntity);
log.info(String.format("Created productStock: %s", productStockEntity));

// Create PartnerProductStock
semiconductorMaterial = materialService.findByOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber());
Expand All @@ -222,7 +235,8 @@ private void setupCustomerRole() throws JsonProcessingException {
log.info("SAMM-DTO:\n" + objectMapper.writeValueAsString(productStockSammDto));

log.info("Own Street and Number: " + variablesService.getOwnDefaultStreetAndNumber());
Partner mySelf = partnerService.getOwnPartnerEntity();

log.info(mySelf.toString());
var builder = MaterialItemStock.builder();
var materialItemStock = builder.partner(supplierPartner)
.material(semiconductorMaterial)
Expand All @@ -234,22 +248,83 @@ private void setupCustomerRole() throws JsonProcessingException {
.build();
var createdMaterialItemStock = materialItemStockService.create(materialItemStock);
log.info("Created MaterialItemStock: \n" + createdMaterialItemStock.toString());

var builder2 = ReportedMaterialItemStock.builder();
var reportedMaterialItemStock =
builder2
.material(semiconductorMaterial)
Site newSite = new Site();
newSite.setBpns("BPNS4444444444DD");
newSite.setName("Dummy Site");
Address address = new Address("BPNA4444444444DD", "Feldweg 1", "54545 Neustadt", "Germany");
newSite.getAddresses().add(address);
mySelf.getSites().add(newSite);
mySelf = partnerService.update(mySelf);

builder = MaterialItemStock.builder();
var otherMaterialItemStock =
builder
.partner(supplierPartner)
.material(semiconductorMaterial)
.lastUpdatedOnDateTime(new Date())
.locationBpns(supplierPartner.getSites().first().getBpns())
.locationBpna(supplierPartner.getSites().first().getAddresses().first().getBpna())
.locationBpns(newSite.getBpns())
.locationBpna(address.getBpna())
.measurementUnit(ItemUnitEnumeration.UNIT_PIECE)
.quantity(50)
.quantity(12)
.build();
otherMaterialItemStock = materialItemStockService.create(otherMaterialItemStock);

var createdReportedMaterialItemStock = reportedMaterialItemStockService.create(reportedMaterialItemStock);
log.info("Created ReportedMaterialItemStock: \n" + createdReportedMaterialItemStock);

var thirdMaterialItemStock = MaterialItemStock.builder()
.partner(supplierPartner)
.material(semiconductorMaterial)
.lastUpdatedOnDateTime(new Date())
.locationBpna(address.getBpna())
.locationBpns(newSite.getBpns())
.measurementUnit(ItemUnitEnumeration.UNIT_PIECE)
.quantity(23)
.isBlocked(true)
.build();
thirdMaterialItemStock = materialItemStockService.create(thirdMaterialItemStock);

var samm = itemStockSammMapper.materialItemStocksToItemStockSamm(List.of(createdMaterialItemStock, otherMaterialItemStock, thirdMaterialItemStock));
var node = objectMapper.readTree(objectMapper.writeValueAsString(samm));
log.info("Created SAMM\n" + node.toPrettyString());

mySelf.getSites().remove(newSite);
mySelf = partnerService.update(mySelf);
ItemStockSamm itemStockSAMM = new ItemStockSamm();
itemStockSAMM.setMaterialNumberSupplier(semiconductorMatNbrSupplier);
itemStockSAMM.setMaterialNumberCustomer(semiconductorMatNbrCustomer);
itemStockSAMM.setDirection(DirectionCharacteristic.OUTBOUND);
var posList = new ArrayList<Position>();
itemStockSAMM.setPositions(posList);
var pos1 = new Position();
posList.add(pos1);
pos1.setLastUpdatedOnDateTime(new Date());
ItemQuantityEntity itemQuantityEntity = new ItemQuantityEntity(31.0, ItemUnitEnumeration.UNIT_PIECE);
var allocatedStocks = new ArrayList<AllocatedStock>();
pos1.setAllocatedStocks(allocatedStocks);
var allocatedStock = new AllocatedStock(itemQuantityEntity, "BPNS123456789012", true, "BPNA123456789012");
allocatedStocks.add(allocatedStock);
itemQuantityEntity = new ItemQuantityEntity(67.0, ItemUnitEnumeration.UNIT_KILOGRAM);
allocatedStock = new AllocatedStock(itemQuantityEntity, "BPNS123456789012", false, "BPNA123456789012");
allocatedStocks.add(allocatedStock);
var pos2 = new Position();
pos2.setLastUpdatedOnDateTime(new Date());
var opr = new OrderPositionReference("123", "234", "345");
pos2.setOrderPositionReference(opr);
allocatedStocks = new ArrayList<>();
pos2.setAllocatedStocks(allocatedStocks);
itemQuantityEntity = new ItemQuantityEntity(43.1, ItemUnitEnumeration.UNIT_KILOGRAM);
allocatedStock = new AllocatedStock(itemQuantityEntity, "BPNS1234567890AB", true, "BPNS1234567890CD");
allocatedStocks.add(allocatedStock);
itemQuantityEntity = new ItemQuantityEntity(83.7, ItemUnitEnumeration.UNIT_CUBIC_CENTIMETRE);
allocatedStock = new AllocatedStock(itemQuantityEntity, "BPNS1234567890EF", false, "BPNS1234567890GH");
allocatedStocks.add(allocatedStock);
posList.add(pos2);

log.info("Sample SAMM: " + objectMapper.readTree(objectMapper.writeValueAsString(itemStockSAMM)));

log.info("To MaterialItemStockList: ");
var list = itemStockSammMapper.itemStockSammToReportedMaterialItemStock(itemStockSAMM, supplierPartner);
for (var s : list) {
log.info(s.toString());
}
}

/**
Expand All @@ -258,6 +333,7 @@ private void setupCustomerRole() throws JsonProcessingException {
private void setupSupplierRole() {
Partner customerPartner = createAndGetCustomerPartner();
Material semiconductorMaterial = getNewSemiconductorMaterialForSupplier();
Partner mySelf = partnerService.getOwnPartnerEntity();

semiconductorMaterial = materialService.create(semiconductorMaterial);
log.info(String.format("Created product: %s", semiconductorMaterial));
Expand All @@ -283,7 +359,7 @@ private void setupSupplierRole() {
semiconductorMaterial,
20,
MeasurementUnit.piece,
"BPNS1234567890ZZ",
mySelf.getSites().first().getBpns(),
LocationIdTypeEnum.B_P_N_S,
new Date(),
customerPartner
Expand Down Expand Up @@ -358,6 +434,8 @@ private Partner createAndGetNonScenarioCustomer() {
"Non-Scenario Customer",
"http://nonscenario-customer.com/api/v1/dsp",
"BPNL2222222222RR",
"BPNS2222222222XY",
"Non Scneario Site",
"BPNA2222222222XZ",
"Fichtenweg 23",
"65432 Waldhausen",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootApplication
public class PurisApplication {

Expand All @@ -43,4 +46,9 @@ public ModelMapper getModelMapper() {
return new ModelMapper();
}

@Bean
public ExecutorService getExecutorService() {
return Executors.newCachedThreadPool();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ private Response sendPostRequest(JsonNode requestBody, List<String> pathSegments

/**
* Call this method at startup to register the necessary request and
* response apis, including unrestricted policy- and contract-definitions
* that allow any external Partner to contract them.
* response apis.
*
* @return true if all registrations were successful, otherwise false
*/
Expand All @@ -115,54 +114,67 @@ public boolean registerAssetsInitially() {
log.info("Registration of product-stock request api successful " + (result = registerApiAsset(DT_ApiMethodEnum.REQUEST)));
if (!result) return false;
log.info("Registration of product-stock response api successful " + (result = registerApiAsset(DT_ApiMethodEnum.RESPONSE)));
if (!result) return false;
log.info("Registration of policy successful " + (result = registerPublicPolicy()));
if (!result) return false;
log.info("Registration of contract definition successful " + (result = registerPublicContractDefinition()));
return result;
}


/**
* Util method to register a public contract definition without restrictions
* regarding the asset selector. Will therefore be applicable to all assets
* that were registered previously. Must be called after registerPublicPolicy()
* Utility method to register policy- and contract-definitions for both the
* REQUEST and the RESPONSE-Api specifically for the given partner.
* @param partner the partner
* @return true, if all registrations ran successfully
*/
public boolean createPolicyAndContractDefForPartner(Partner partner) {
boolean result = createPolicyDefinitionForPartner(partner);
result &= createContractDefinitionForPartner(partner, DT_ApiMethodEnum.REQUEST);
return result & createContractDefinitionForPartner(partner, DT_ApiMethodEnum.RESPONSE);
}

/**
* Registers a contract definition using a policy that
* allows only the given partner's BPNL for the given Api Method.
* Prior to this method you must always have successfully completed the
* createPolicyDefinitionForPartner - method first.
*
* @return true if successful
* @param partner the partner
* @param apiMethod the api method
* @return true, if registration ran successfully
*/
private boolean registerPublicContractDefinition() {
var body = edcRequestBodyBuilder.buildContractDefinitionWithPublicPolicyBody();
private boolean createContractDefinitionForPartner(Partner partner, DT_ApiMethodEnum apiMethod) {
var body = edcRequestBodyBuilder.buildContractDefinitionWithBpnRestrictedPolicy(partner, apiMethod);
try {
var response = sendPostRequest(body, List.of("v2", "contractdefinitions"));
boolean result = response.isSuccessful();
if (!result) {
log.warn("Contract definition registration failed \n" + response.body().string());
if(!result) {
log.warn("Contract definition registration failed for partner " + partner.getBpnl() + " and "
+ apiMethod + "\n" + response.body().string());
}
response.body().close();
return result;
} catch (Exception e) {
log.error("Failed to register contract definition ", e);
return false;
}
}

/**
* Util method to register a policy without any restrictions to
* your control plane. Should be called after asset creation.
* Registers a policy definitions that allows only the given partner's
* BPNL.
*
* @return true if successful
* @param partner the partner
* @return true, if registration ran successfully
*/
private boolean registerPublicPolicy() {
var body = edcRequestBodyBuilder.buildPublicPolicyBody();
private boolean createPolicyDefinitionForPartner(Partner partner) {
var body = edcRequestBodyBuilder.buildBpnRestrictedPolicy(partner);
try {
var response = sendPostRequest(body, List.of("v2", "policydefinitions"));
boolean result = response.isSuccessful();
if (!result) {
log.warn("Policy registration failed \n" + response.body().string());
if(!result){
log.warn("Policy Registration failed \n" + response.body().string());
}
response.body().close();
return result;
} catch (Exception e) {
log.error("Failed to register policy definition ", e);
log.error("Failed to register policy definition for partner " + partner.getBpnl(), e);
return false;
}
}
Expand Down
Loading

0 comments on commit e5e5193

Please sign in to comment.