diff options
author | bt2983 <bt2983@att.com> | 2020-03-02 14:02:39 -0700 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2020-03-04 20:05:51 +0000 |
commit | 1ba9e49a2bedaee3e7a6cfbfdab7a0416a01ee20 (patch) | |
tree | 093b28bb1eac2bd87c902832ba52823ab70e81a4 /ms/neng/src/main/java | |
parent | ec939496820c47e41630b1572e5002c4c4d8da7b (diff) |
Naming micro-service - upgrade to policy decision API.
Issue-ID: CCSDK-1307
Signed-off-by: bt2983 <bt2983@att.com>
Change-Id: I03cb725a2ef5c187ce41a9d5ea242684fdcbc0f3
Diffstat (limited to 'ms/neng/src/main/java')
7 files changed, 209 insertions, 27 deletions
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<String, Object> configAttributes; - private static String configName; - private static String ecompName; - private static String policyName; + private Map<String, Object> configAttributes; + private String configName; + private String ecompName; + private String policyName; boolean unique; public Map<String, Object> 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<String,Object> 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<String, Object> getResource() { + return resource; + } + + public void setResource(Map<String, Object> 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 { + <T, R> GetConfigResponse makeOutboundCall( String policyName, T request, Class<R> 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<Map<Object, Object>> respObj = objectmapper.readValue(policyDetails.getPolicyResponse(), - new TypeReference<List<Map<Object, Object>>>() {}); - 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<String,Object> 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)); } - <T, R> GetConfigResponse makeOutboundCall(T request, Class<R> response) throws Exception { + <T, R> GetConfigResponse makeOutboundCall( String policyName, T request, Class<R> response) throws Exception { log.info("Policy Manager - " + policManProps.getUrl()); + RequestEntity<T> re = RequestEntity.post(new URI(policManProps.getUrl())) .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(request); try { ResponseEntity<Object> 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<Map<Object, Object>> respObj = objectmapper.readValue( - objectmapper.writeValueAsString(resp.getBody()), - new TypeReference<List<Map<Object, Object>>>() {}); - 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<Object, Object> respObj = objectmapper.readValue( body, new TypeReference<Map<Object, Object>>() {}); + List<Map<Object, Object>> respList = transformConfigObjectV2(objectmapper, respObj); + getConfigResp.setResponse(respList); + } catch ( Exception e ) { + List<Map<Object, Object>> respObj = objectmapper.readValue( body, new TypeReference<List<Map<Object, Object>>>() {}); + transformConfigObject(objectmapper, respObj); + getConfigResp.setResponse(respObj); + } + return getConfigResp; } void handleError(HttpStatusCodeException e) throws Exception { @@ -128,6 +185,36 @@ public class PolicyFinderServiceImpl implements PolicyFinder { } /** + * Transforms the policy-V2 response in a form compatible with V1. + */ + List<Map<Object,Object>> transformConfigObjectV2(ObjectMapper objectmapper, Map<Object, Object> respObj) throws Exception { + List<Map<Object,Object>> policyList = new ArrayList<>(); + + Object policies = respObj.get("policies"); + if (policies != null && policies instanceof Map<?, ?> ) { + Map<Object, Object> policiesMap = (Map<Object,Object>)policies; + if ( policiesMap.size() > 0 ) { + Object policy = policiesMap.entrySet().iterator().next().getValue(); + if ( policy != null && policy instanceof Map<?, ?> ) { + Map<Object, Object> thePolicyMap = (Map<Object,Object>)policy; + Object properties = thePolicyMap.get("properties"); + if ( properties != null && properties instanceof Map<?, ?> ) { + Map<Object, Object> propertiesMap = (Map<Object,Object>)properties; + + Map<Object,Object> top = new HashMap<>(); + Map<Object,Object> 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. */ void transformConfigObject(ObjectMapper objectmapper, List<Map<Object, Object>> respObj) throws Exception { |