From 1ba9e49a2bedaee3e7a6cfbfdab7a0416a01ee20 Mon Sep 17 00:00:00 2001 From: bt2983 Date: Mon, 2 Mar 2020 14:02:39 -0700 Subject: Naming micro-service - upgrade to policy decision API. Issue-ID: CCSDK-1307 Signed-off-by: bt2983 Change-Id: I03cb725a2ef5c187ce41a9d5ea242684fdcbc0f3 --- .../neng/core/resource/model/GetConfigRequest.java | 8 +- .../core/resource/model/GetConfigRequestV2.java | 87 ++++++++++++++++ .../core/resource/model/GetConfigResponse.java | 2 +- .../ms/neng/core/service/SpringServiceImpl.java | 1 + .../ms/neng/extinf/props/PolicyManagerProps.java | 13 +++ .../extinf/impl/PolicyFinderServiceDbImpl.java | 10 +- .../extinf/impl/PolicyFinderServiceImpl.java | 115 ++++++++++++++++++--- .../extinf/impl/PolicyFinderServiceImplTest.java | 14 +-- 8 files changed, 217 insertions(+), 33 deletions(-) create mode 100644 ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequestV2.java diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequest.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequest.java index c233a28b..b3953d7e 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequest.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequest.java @@ -29,10 +29,10 @@ import java.util.Map; public class GetConfigRequest implements Serializable { private static final long serialVersionUID = -8039686696076337053L; - private static Map configAttributes; - private static String configName; - private static String ecompName; - private static String policyName; + private Map configAttributes; + private String configName; + private String ecompName; + private String policyName; boolean unique; public Map getConfigAttributes() { diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequestV2.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequestV2.java new file mode 100644 index 00000000..159d80a8 --- /dev/null +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequestV2.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK.apps + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://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. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.apps.ms.neng.core.resource.model; + +import java.io.Serializable; +import java.util.Map; + +/** + * POJO representing policy manager get-config request, V2. + */ +public class GetConfigRequestV2 implements Serializable { + private static final long serialVersionUID = -8039686696076337054L; + + private String onapName; + private String onapComponent; + private String onapInstance; + private String requestId; + private String action; + private Map resource; + + public String getOnapName() { + return onapName; + } + + public void setOnapName(String onapName) { + this.onapName = onapName; + } + + public String getOnapComponent() { + return onapComponent; + } + + public void setOnapComponent(String onapComponent) { + this.onapComponent = onapComponent; + } + + public String getOnapInstance() { + return onapInstance; + } + + public void setOnapInstance(String onapInstance) { + this.onapInstance = onapInstance; + } + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + public Map getResource() { + return resource; + } + + public void setResource(Map resource) { + this.resource = resource; + } + +} diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigResponse.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigResponse.java index f8ea6e1e..af1b58ae 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigResponse.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigResponse.java @@ -27,7 +27,7 @@ import java.io.Serializable; */ public class GetConfigResponse implements Serializable { private static final long serialVersionUID = -8039686696076337053L; - private static Object response; + private Object response; public Object getResponse() { return response; diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java index 347a2393..3772fd71 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java @@ -135,6 +135,7 @@ public class SpringServiceImpl implements SpringService { if (e instanceof NengException) { throw e; } else { + e.printStackTrace(); log.warning(e.getMessage()); throw new Exception("Internal error occurred while processing the request"); } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/extinf/props/PolicyManagerProps.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/extinf/props/PolicyManagerProps.java index bd06f76c..e84d5b69 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/extinf/props/PolicyManagerProps.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/extinf/props/PolicyManagerProps.java @@ -32,6 +32,7 @@ public class PolicyManagerProps { String clientAuth; String basicAuth; String url; + String version; String environment; String ecompRequestId; @@ -89,4 +90,16 @@ public class PolicyManagerProps { public void setEcompRequestId(String ecompRequestId) { this.ecompRequestId = ecompRequestId; } + + /** + * Version of the policy manager -- '1' for the First version '2' for second version etc. + */ + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceDbImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceDbImpl.java index 9cb434ff..0ffb2aae 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceDbImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceDbImpl.java @@ -45,18 +45,12 @@ public class PolicyFinderServiceDbImpl extends PolicyFinderServiceImpl { * Finds the policy with the given name from the DB. */ @Override - public GetConfigResponse getConfig(String policyName) throws Exception { + GetConfigResponse makeOutboundCall( String policyName, T request, Class response) throws Exception { ObjectMapper objectmapper = new ObjectMapper(); - objectmapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); PolicyDetails policyDetails = policyDetailsRepo.findPolicyResponseByName(policyName); if (policyDetails == null) { throw new Exception("Unable to find the policy " + policyName); } - List> respObj = objectmapper.readValue(policyDetails.getPolicyResponse(), - new TypeReference>>() {}); - transformConfigObject(objectmapper, respObj); - GetConfigResponse configResp = new GetConfigResponse(); - configResp.setResponse(respObj); - return configResp; + return handleResponse( policyDetails.getPolicyResponse() ); } } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImpl.java index ce7e3fd5..d577dc4b 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImpl.java @@ -26,8 +26,11 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.net.URI; import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.logging.Logger; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; @@ -39,6 +42,7 @@ import org.apache.http.impl.client.HttpClients; import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException; import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequest; +import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigRequestV2; import org.onap.ccsdk.apps.ms.neng.core.resource.model.GetConfigResponse; import org.onap.ccsdk.apps.ms.neng.core.rs.interceptors.PolicyManagerAuthorizationInterceptor; import org.onap.ccsdk.apps.ms.neng.extinf.props.PolicyManagerProps; @@ -82,34 +86,87 @@ public class PolicyFinderServiceImpl implements PolicyFinder { } } + protected boolean shouldUsePolicyV2 () { + String version = policManProps.getVersion(); + log.info("Policy Manager Version - " + version ); + + try { + int vnum = Integer.parseInt(version); + if ( vnum <= 1 ) { + return false; + } + } catch ( Exception e ) { + return true; + } + + return true; + } + GetConfigResponse getConfig(String policyName) throws Exception { - GetConfigRequest getConfigRequest = new GetConfigRequest(); - getConfigRequest.setPolicyName(policyName); - return (makeOutboundCall(getConfigRequest, GetConfigResponse.class)); + + Object request; + if ( shouldUsePolicyV2() ) { + GetConfigRequestV2 req = new GetConfigRequestV2(); + + req.setOnapName("SDNC"); + req.setOnapComponent("CCSDK"); + req.setOnapInstance("CCSDK-ms-neng"); + req.setRequestId( UUID.randomUUID().toString() ); + req.setAction("naming"); + + Map resource = new HashMap<>(); + resource.put("policy-id", policyName); + req.setResource(resource); + + request = req; + } else { + GetConfigRequest getConfigRequest = new GetConfigRequest(); + + getConfigRequest.setPolicyName(policyName); + + request = getConfigRequest; + } + + ObjectMapper reqmapper = new ObjectMapper(); + String reqStr = reqmapper.writeValueAsString(request); + log.info("Request - " + reqStr); + + return (makeOutboundCall( policyName, request, GetConfigResponse.class)); } - GetConfigResponse makeOutboundCall(T request, Class response) throws Exception { + GetConfigResponse makeOutboundCall( String policyName, T request, Class response) throws Exception { log.info("Policy Manager - " + policManProps.getUrl()); + RequestEntity re = RequestEntity.post(new URI(policManProps.getUrl())) .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(request); try { ResponseEntity resp = getRestTemplate().exchange(re, Object.class); if (HttpStatus.OK.equals(resp.getStatusCode())) { ObjectMapper objectmapper = new ObjectMapper(); - log.info(objectmapper.writeValueAsString(resp.getBody())); - //System.out.println(objectmapper.writeValueAsString(resp.getBody())); - List> respObj = objectmapper.readValue( - objectmapper.writeValueAsString(resp.getBody()), - new TypeReference>>() {}); - transformConfigObject(objectmapper, respObj); - GetConfigResponse getConfigResp = new GetConfigResponse(); - getConfigResp.setResponse(respObj); - return getConfigResp; + String bodyStr = objectmapper.writeValueAsString(resp.getBody()); + return handleResponse( bodyStr ); } } catch (HttpStatusCodeException e) { handleError(e); } - throw new NengException("Error while retrieving policy from policy manager."); + throw new NengException("Error while retrieving policy " + policyName +" from policy manager."); + } + + GetConfigResponse handleResponse ( String body ) throws Exception { + log.info(body); + + ObjectMapper objectmapper = new ObjectMapper(); + GetConfigResponse getConfigResp = new GetConfigResponse(); + try { + Map respObj = objectmapper.readValue( body, new TypeReference>() {}); + List> respList = transformConfigObjectV2(objectmapper, respObj); + getConfigResp.setResponse(respList); + } catch ( Exception e ) { + List> respObj = objectmapper.readValue( body, new TypeReference>>() {}); + transformConfigObject(objectmapper, respObj); + getConfigResp.setResponse(respObj); + } + return getConfigResp; } void handleError(HttpStatusCodeException e) throws Exception { @@ -127,6 +184,36 @@ public class PolicyFinderServiceImpl implements PolicyFinder { throw new NengException("Error while retrieving policy from policy manager."); } + /** + * Transforms the policy-V2 response in a form compatible with V1. + */ + List> transformConfigObjectV2(ObjectMapper objectmapper, Map respObj) throws Exception { + List> policyList = new ArrayList<>(); + + Object policies = respObj.get("policies"); + if (policies != null && policies instanceof Map ) { + Map policiesMap = (Map)policies; + if ( policiesMap.size() > 0 ) { + Object policy = policiesMap.entrySet().iterator().next().getValue(); + if ( policy != null && policy instanceof Map ) { + Map thePolicyMap = (Map)policy; + Object properties = thePolicyMap.get("properties"); + if ( properties != null && properties instanceof Map ) { + Map propertiesMap = (Map)properties; + + Map top = new HashMap<>(); + Map config = new HashMap<>(); + top.put("config", config ); + config.put("content", propertiesMap ); + policyList.add(top); + } + } + } + } + return policyList; + } + + /** * Transforms the 'config' element (which is received as a JSON string) to a map like a JSON object. */ diff --git a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImplTest.java b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImplTest.java index b33550a3..7ce03cc5 100644 --- a/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImplTest.java +++ b/ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImplTest.java @@ -71,13 +71,15 @@ public class PolicyFinderServiceImplTest { @Test public void testConfig() throws Exception { - doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any()); + doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( + Matchers.any(), Matchers.any(), Matchers.any()); assertNotNull(policyFinder.getConfig("policy")); } @Test public void testFindPolicy() throws Exception { - doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall(Matchers.any(), Matchers.any()); + doReturn(new GetConfigResponse()).when(policyFinder).makeOutboundCall( + Matchers.any(), Matchers.any(), Matchers.any()); assertNull(policyFinder.findPolicy("policy")); } @@ -100,7 +102,7 @@ public class PolicyFinderServiceImplTest { GetConfigRequest request = new GetConfigRequest(); request.setPolicyName("policy"); - GetConfigResponse configResp = policyFinder.makeOutboundCall(request, GetConfigResponse.class); + GetConfigResponse configResp = policyFinder.makeOutboundCall("",request, GetConfigResponse.class); assertNotNull(configResp); } @@ -116,7 +118,7 @@ public class PolicyFinderServiceImplTest { GetConfigRequest request = new GetConfigRequest(); request.setPolicyName("policy"); - policyFinder.makeOutboundCall(request, GetConfigResponse.class); + policyFinder.makeOutboundCall("",request, GetConfigResponse.class); } @Test @@ -226,7 +228,7 @@ public class PolicyFinderServiceImplTest { policManProps.setUrl("http://policyManager.onap.org"); GetConfigRequest request = new GetConfigRequest(); request.setPolicyName("policy"); - policyFinder.makeOutboundCall(request, GetConfigResponse.class); + policyFinder.makeOutboundCall("", request, GetConfigResponse.class); } @SuppressWarnings("unchecked") @@ -237,7 +239,7 @@ public class PolicyFinderServiceImplTest { policManProps.setUrl("http://policyManager.onap.org"); GetConfigRequest request = new GetConfigRequest(); request.setPolicyName("policy"); - policyFinder.makeOutboundCall(request, GetConfigResponse.class); + policyFinder.makeOutboundCall("", request, GetConfigResponse.class); } @Test -- cgit 1.2.3-korg