Naming micro-service - upgrade to policy decision API. 77/102777/2
authorbt2983 <bt2983@att.com>
Mon, 2 Mar 2020 21:02:39 +0000 (14:02 -0700)
committerDan Timoney <dtimoney@att.com>
Wed, 4 Mar 2020 20:05:51 +0000 (20:05 +0000)
Issue-ID: CCSDK-1307

Signed-off-by: bt2983 <bt2983@att.com>
Change-Id: I03cb725a2ef5c187ce41a9d5ea242684fdcbc0f3

ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequest.java
ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigRequestV2.java [new file with mode: 0644]
ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/resource/model/GetConfigResponse.java
ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java
ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/extinf/props/PolicyManagerProps.java
ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceDbImpl.java
ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImpl.java
ms/neng/src/test/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/PolicyFinderServiceImplTest.java

index c233a28..b3953d7 100644 (file)
@@ -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 (file)
index 0000000..159d80a
--- /dev/null
@@ -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;
+    }
+
+}
index f8ea6e1..af1b58a 100644 (file)
@@ -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;
index 347a239..3772fd7 100644 (file)
@@ -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");
             }
index bd06f76..e84d5b6 100644 (file)
@@ -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;
+    }
+
 }
index 9cb434f..0ffb2aa 100644 (file)
@@ -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() );
     }
 }
index ce7e3fd..d577dc4 100644 (file)
@@ -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 {
@@ -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<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.
      */
index b33550a..7ce03cc 100644 (file)
@@ -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