Skip to content

Commit

Permalink
Added utitlty functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinargade123 committed May 24, 2024
1 parent 72e5d20 commit b844ce6
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import org.eclipse.tractusx.autosetup.model.Customer;
import org.eclipse.tractusx.autosetup.model.SelectedTools;
import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy;
//import org.eclipse.tractusx.autosetup.utility.JsonObjectProcessingUtility;
//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.eclipse.tractusx.autosetup.utility.JsonObjectProcessingUtility;
import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.eclipse.tractusx.autosetup.utility.LogUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.retry.annotation.Backoff;
Expand Down Expand Up @@ -77,7 +77,7 @@ public class ConnectorRegistrationManager {

private final AutoSetupTriggerManager autoSetupTriggerManager;
private final PortalIntegrationProxy portalIntegrationProxy;
//private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;
private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;

@Retryable(retryFor = {
ServiceException.class }, maxAttemptsExpression = "${retry.maxAttempts}", backoff = @Backoff(delayExpression = "#{${retry.backOffDelay}}"))
Expand Down Expand Up @@ -106,7 +106,7 @@ public Map<String, String> registerConnector(Customer customerDetails, SelectedT
body.add(SUBSCRIPTION_ID, subscriptionIdVal);
Map<String, String> header = new HashMap<>();
header.put("Authorization",
"Bearer " + "");
"Bearer " + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI));

String connectorId = checkSubcriptionHaveConnectorRegister(header, subscriptionIdVal);

Expand Down Expand Up @@ -175,13 +175,13 @@ private String checkSubcriptionHaveConnectorRegister(Map<String, String> header,
if (subcriptionWithConnectors != null && subcriptionWithConnectors.isArray()) {
for (JsonNode jsonNode : subcriptionWithConnectors) {

String remoteSubscriptionId = "";//JsonObjectProcessingUtility.getValueFromJsonNode(jsonNode,
//SUBSCRIPTION_ID);
String remoteSubscriptionId = JsonObjectProcessingUtility.getValueFromJsonNode(jsonNode,
SUBSCRIPTION_ID);

if (subscriptionId.equalsIgnoreCase(remoteSubscriptionId) && jsonNode!=null) {
if (subscriptionId.equalsIgnoreCase(remoteSubscriptionId)) {

JsonNode connectorIds = null;//JsonObjectProcessingUtility.getArrayNodeFromJsonNode(jsonNode,
//"connectorIds");
JsonNode connectorIds = JsonObjectProcessingUtility.getArrayNodeFromJsonNode(jsonNode,
"connectorIds");

if (connectorIds != null && connectorIds.isArray() && connectorIds.size() > 0)
return connectorIds.get(0).asText();
Expand Down Expand Up @@ -214,7 +214,7 @@ public Map<String, String> deleteConnector(SelectedTools tool, Map<String, Strin

Map<String, String> header = new HashMap<>();
header.put("Authorization",
"Bearer " + "");
"Bearer " + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI));

autoSetupTriggerDetails.setStatus(TriggerStatusEnum.SUCCESS.name());
portalIntegrationProxy.deleteConnector(connectorRegistrationUrl, header, connectorId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.eclipse.tractusx.autosetup.portal.model.TechnicalUserDetails;
import org.eclipse.tractusx.autosetup.portal.model.TechnicalUsers;
import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy;
//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.eclipse.tractusx.autosetup.utility.LogUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.retry.annotation.Backoff;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class PortalIntegrationManager {

private final AutoSetupTriggerManager autoSetupTriggerManager;

//private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;
private final KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;

@Value("${portal.url}")
private URI portalUrl;
Expand Down Expand Up @@ -115,7 +115,7 @@ public Map<String, String> postServiceInstanceResultAndGetTenantSpecs(Customer c

Map<String, String> header = new HashMap<>();
header.put(AUTHORIZATION,
BEARER + "");
BEARER + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI));

ServiceInstanceResultRequest serviceInstanceResultRequest = ServiceInstanceResultRequest.builder()
.requestId(subscriptionId).offerUrl(applicationURL).build();
Expand Down Expand Up @@ -248,7 +248,7 @@ private ServiceInstanceResultResponse verifyIsAlreadySubcribedActivatedAndGetDet
try {

header.put(AUTHORIZATION,
BEARER + "");
BEARER + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI));

serviceInstanceResultResponse = portalIntegrationProxy.getAppServiceInstanceSubcriptionDetails(
portalUrl, header, appServiceURIPath, offerId, subscriptionId);
Expand Down Expand Up @@ -292,7 +292,7 @@ private void readTechnicalUserDetails(Map<String, String> header,
if (serviceInstanceResultResponse.getTechnicalUserData() != null) {

header.put(AUTHORIZATION,
BEARER + "");
BEARER + keyCloakTokenProxyUtitlity.getKeycloakToken(clientId, clientSecret, tokenURI));

serviceInstanceResultResponse.getTechnicalUserData().forEach(elel -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/********************************************************************************
* 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.autosetup.utility;

import com.fasterxml.jackson.databind.JsonNode;

import lombok.SneakyThrows;


public class JsonObjectProcessingUtility {

private JsonObjectProcessingUtility() {}

@SneakyThrows
public static String getValueFromJsonNode(JsonNode jsonNode, String propertyId) {
if (jsonNode != null && jsonNode.get(propertyId) != null)
return jsonNode.get(propertyId).asText();
else
return "";
}


@SneakyThrows
public static JsonNode getArrayNodeFromJsonNode(JsonNode jsonnode, String propertyId) {
if (jsonnode != null && jsonnode.get(propertyId) != null)
return jsonnode.get(propertyId);
else
return null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/********************************************************************************
* 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.autosetup.utility;

import java.net.URI;

import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

@Service
@RequiredArgsConstructor
public class KeyCloakTokenProxyUtitlity {

private final PortalIntegrationProxy portalIntegrationProxy;

@SneakyThrows
public String getKeycloakToken(String clientId, String clientSecret, URI tokenURI) {

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("grant_type", "client_credentials");
body.add("client_id", clientId);
body.add("client_secret", clientSecret);
var resultBody = portalIntegrationProxy.readAuthToken(tokenURI, body);

if (resultBody != null) {
return resultBody.getAccessToken();
}
return null;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eclipse.tractusx.autosetup.model.SelectedTools;
import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy;
import org.eclipse.tractusx.autosetup.utility.Certutil;
//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockito.InjectMocks;
Expand Down Expand Up @@ -67,8 +67,8 @@ class ConnectorRegistrationManagerTest {
@InjectMocks
private AutoSetupTriggerEntry triger;

// @Mock
// private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;
@Mock
private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;

@Test
void createClient() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.eclipse.tractusx.autosetup.mapper.AutoSetupRequestMapper;
import org.eclipse.tractusx.autosetup.model.AutoSetupRequest;
import org.eclipse.tractusx.autosetup.portal.proxy.PortalIntegrationProxy;
//import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.eclipse.tractusx.autosetup.utility.KeyCloakTokenProxyUtitlity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -56,8 +56,8 @@ class AutoSetupOrchitestratorServiceTest {
@MockBean
private KubeAppManageProxy kubeAppManageProxy;

// @MockBean
// private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;
@MockBean
private KeyCloakTokenProxyUtitlity keyCloakTokenProxyUtitlity;

@Test
void createPackage() {
Expand Down

0 comments on commit b844ce6

Please sign in to comment.