Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat|sde-backend] : EDC API's updation and other refactor classes. #111

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added
- Added controller interface api's for Policy management.
- External EDC service interface api updated.
- Updated supported sub-model implementation classes.


## [2.3.6] - 2024-03-06
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ maven/mavencentral/org.aspectj/aspectjweaver/1.9.19, Apache-2.0 AND BSD-3-Clause
maven/mavencentral/org.aspectj/aspectjweaver/1.9.20.1, Apache-2.0 AND BSD-3-Clause AND EPL-1.0 AND BSD-3-Clause AND Apache-1.1, approved, #7695
maven/mavencentral/org.aspectj/aspectjweaver/1.9.21, Apache-2.0 AND BSD-3-Clause AND EPL-1.0 AND BSD-3-Clause AND Apache-1.1, approved, #7695
maven/mavencentral/org.assertj/assertj-core/3.24.2, Apache-2.0, approved, #6161
maven/mavencentral/org.awaitility/awaitility/4.2.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.awaitility/awaitility/4.2.0, Apache-2.0, approved, #14178
maven/mavencentral/org.bouncycastle/bcpkix-jdk15on/1.69, MIT, approved, clearlydefined
maven/mavencentral/org.bouncycastle/bcprov-jdk15on/1.69, MIT, approved, clearlydefined
maven/mavencentral/org.bouncycastle/bcprov-jdk18on/1.77, MIT AND CC0-1.0, approved, #11595
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/********************************************************************************
* Copyright (c) 2022, 2023 T-Systems International GmbH
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 T-Systems International GmbH
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -36,6 +36,10 @@ public void init(JsonObject submodelSchema) {
this.submodelSchema = submodelSchema;
}

public String getNameOfModel() {
return submodelSchema.get("id").getAsString();
}

public JsonObject getSubmodelItems() {
return submodelSchema.get("items").getAsJsonObject();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/********************************************************************************
* Copyright (c) 2023 T-Systems International GmbH
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 T-Systems International GmbH
* 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 Down Expand Up @@ -33,38 +33,70 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
@RequiredArgsConstructor
public class TokenUtility {

private static final String CLIENT_ID = "client_id";
private static final String CLIENT_SECRET = "client_secret";
private static final String GRANT_TYPE = "grant_type";
private static final String SCOPE = "scope";

private final ITokenUtilityProxy tokenUtilityProxy;

@SneakyThrows
public String getToken(URI appTokenURI, String grantType, String appClientId, String appClientSecret) {
try {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add(GRANT_TYPE, grantType);
body.add(CLIENT_ID, appClientId);
body.add(CLIENT_SECRET, appClientSecret);

MultiValueMap<String, Object> body = getMultiValueBody(grantType, appClientId, appClientSecret);

return createToken(appTokenURI, body);

}

@SneakyThrows
public String getToken(URI appTokenURI, String grantType, String appClientId, String appClientSecret,
String scope) {

MultiValueMap<String, Object> body = getMultiValueBody(grantType, appClientId, appClientSecret);
body.add(SCOPE, scope);

return createToken(appTokenURI, body);

}

private MultiValueMap<String, Object> getMultiValueBody(String grantType, String appClientId,
String appClientSecret) {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add(GRANT_TYPE, grantType);
body.add(CLIENT_ID, appClientId);
body.add(CLIENT_SECRET, appClientSecret);
return body;
}

private String createToken(URI appTokenURI, MultiValueMap<String, Object> body) {

try {
var resultBody = tokenUtilityProxy.getToken(appTokenURI, body);

if (resultBody != null)
return resultBody.getAccessToken();
else
throw new ServiceException(
"Unable to get auth token because auth response resultBody is: " + resultBody);
} catch (FeignException e) {
log.error("FeignException RequestBody : " + e.request());
String errorMsg = "Error in DT twin lookup " + e.request().url() + ", because: " + body+ "," + e.contentUTF8();
log.error("FeignException : " + errorMsg);
throw new ServiceException(errorMsg);
} catch (Exception e) {
throw new ServiceException("Unable to process auth request: " + appTokenURI + ", " + e.getMessage());
}

}

public String getOriginalRequestAuthToken() throws ServiceException {
Expand All @@ -87,7 +119,7 @@ public boolean isTokenValid(String accessToken) {
long tokenExpirationTime = actualObj.get("exp").asLong() * 1000;
long currentTime = System.currentTimeMillis();

return tokenExpirationTime - 20000 > currentTime;
return tokenExpirationTime - 40000 > currentTime;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/********************************************************************************
* Copyright (c) 2024 T-Systems International GmbH
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package org.eclipse.tractusx.sde.configuration;

import java.util.List;
import java.util.Map;

import org.eclipse.tractusx.sde.common.entities.PolicyModel;
import org.eclipse.tractusx.sde.common.utils.UUIdGenerator;
import org.eclipse.tractusx.sde.edc.entities.request.asset.AssetEntryRequest;
import org.eclipse.tractusx.sde.edc.entities.request.asset.AssetEntryRequestFactory;
import org.eclipse.tractusx.sde.edc.facilitator.CreateEDCAssetFacilator;
import org.eclipse.tractusx.sde.edc.gateways.external.EDCGateway;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@RequiredArgsConstructor
@Profile("default")
public class PCFExchangeAssetProvider {

private final AssetEntryRequestFactory assetFactory;
private final EDCGateway edcGateway;
private final CreateEDCAssetFacilator createEDCAssetFacilator;

@Value("${dft.hostname}")
private String sdeHostname;

private static String assetFilterRequest = """
{
"@context": {
"edc": "https://w3id.org/edc/v0.0.1/ns/"
},
"@type": "QuerySpec",
"offset": 0,
"limit": 10,
"sortOrder": "DESC",
"sortField": "id",
"filterExpression": [
{
"edc:operandLeft": "https://w3id.org/edc/v0.0.1/ns/type",
"edc:operator": "=",
"edc:operandRight": "data.pcf.exchangeEndpoint"
}
]
}
""";

@PostConstruct
@SneakyThrows
public void init() {

String assetId = UUIdGenerator.getUuid();
AssetEntryRequest assetEntryRequest = assetFactory.getAssetRequest("", "PCF Exchange endpoint information",
assetId, "1", "");

assetEntryRequest.getProperties().put("type", "data.pcf.exchangeEndpoint");
assetEntryRequest.getDataAddress().getProperties().put("baseUrl", sdeHostname+"/pcf");
ObjectNode requestBody = (ObjectNode) new ObjectMapper().readTree(assetFilterRequest);

if (!edcGateway.assetExistsLookupBasedOnType(requestBody)) {

PolicyModel policy= PolicyModel.builder()
.accessPolicies(List.of())
.usagePolicies(List.of())
.build();

Map<String, String> createEDCAsset = createEDCAssetFacilator.createEDCAsset(assetEntryRequest, policy);
log.info("PCF Exchange asset creates :" + createEDCAsset.toString());
} else {
log.info("PCF Exchange asset exists in edc connector, so ignoring asset creation");
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/********************************************************************************
* Copyright (c) 2022, 2023 T-Systems International GmbH
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 T-Systems International GmbH
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -21,6 +21,7 @@
package org.eclipse.tractusx.sde.core.controller;

import java.util.List;
import java.util.Map;

import org.eclipse.tractusx.sde.core.role.entity.RolePojo;
import org.eclipse.tractusx.sde.core.service.RoleManagementService;
Expand All @@ -45,7 +46,7 @@ public class RoleManagementController {

@PostMapping(value = "/role/{role}/permissions")
@PreAuthorize("hasPermission(#role,'create_role')")
public String saveRolePermission(@PathVariable("role") String role, @RequestBody List<String> rolePermission) {
public Map<String,String> saveRolePermission(@PathVariable("role") String role, @RequestBody List<String> rolePermission) {
return roleManagementService.saveRoleWithPermission(role, rolePermission);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/********************************************************************************
* Copyright (c) 2024 T-Systems International GmbH
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package org.eclipse.tractusx.sde.core.failurelog.repository;


import org.eclipse.tractusx.sde.core.processreport.entity.ConsumerDownloadHistoryEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ConsumerDownloadHistoryRepository extends JpaRepository<ConsumerDownloadHistoryEntity, String> {

ConsumerDownloadHistoryEntity findByProcessId(String id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.tractusx.sde.common.exception.NoDataFoundException;
import org.eclipse.tractusx.sde.common.exception.ValidationException;
import org.eclipse.tractusx.sde.common.model.PagingResponse;
import org.eclipse.tractusx.sde.common.utils.LogUtil;
import org.eclipse.tractusx.sde.core.policy.entity.PolicyEntity;
import org.eclipse.tractusx.sde.core.policy.entity.PolicyMapper;
import org.eclipse.tractusx.sde.core.policy.repository.PolicyRepository;
Expand Down Expand Up @@ -68,7 +69,7 @@ public PolicyModel savePolicy(String uuid, PolicyModel request) {
request.setLastUpdatedTime(LocalDateTime.now());
request.setUuid(uuid);
repository.save(policy);
log.info("'" + request.getPolicyName() + "' policy saved in the database successfully");
log.info(LogUtil.encode("'" + request.getPolicyName() + "' policy saved in the database successfully"));
return request;
} else
throw new ValidationException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/********************************************************************************
* Copyright (c) 2022 Critical TechWorks GmbH
* Copyright (c) 2022 BMW GmbH
* Copyright (c) 2022, 2023 T-Systems International GmbH
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 T-Systems International GmbH
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand All @@ -23,8 +23,10 @@
package org.eclipse.tractusx.sde.core.processreport.repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.eclipse.tractusx.sde.common.enums.ProgressStatusEnum;
import org.eclipse.tractusx.sde.core.processreport.entity.ProcessReportEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -35,8 +37,16 @@

public interface ProcessReportRepository extends JpaRepository<ProcessReportEntity, String> {

Optional<ProcessReportEntity> findByProcessIdAndStatus(String processId, ProgressStatusEnum status);

long countByProcessIdInAndStatus(List<String> processIdList, ProgressStatusEnum status);

List<ProcessReportEntity> findByProcessIdIn(List<String> processIds);

Optional<ProcessReportEntity> findByProcessId(String processId);

List<ProcessReportEntity> findByStatus(ProgressStatusEnum status);

@Modifying
@Transactional
@Query(value = "UPDATE process_report " +
Expand Down
Loading
Loading