Naming micro-service - upgrade to policy decision API.

Issue-ID: CCSDK-1307

Signed-off-by: bt2983 <bt2983@att.com>
Change-Id: I03cb725a2ef5c187ce41a9d5ea242684fdcbc0f3
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 c233a28..b3953d7 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 @@
 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 0000000..159d80a
--- /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 f8ea6e1..af1b58a 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 @@
  */
 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 347a239..3772fd7 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 @@
             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 bd06f76..e84d5b6 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 @@
     String clientAuth;
     String basicAuth;
     String url;
+    String version;
     String environment;
     String ecompRequestId;
 
@@ -89,4 +90,16 @@
     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 9cb434f..0ffb2aa 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 @@
      * 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 ce7e3fd..d577dc4 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.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.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 @@
         }
     }
 
-    GetConfigResponse getConfig(String policyName) throws Exception {
-        GetConfigRequest getConfigRequest = new GetConfigRequest();
-        getConfigRequest.setPolicyName(policyName);
-        return (makeOutboundCall(getConfigRequest, GetConfigResponse.class));
+    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;
     }
 
-    <T, R> GetConfigResponse makeOutboundCall(T request, Class<R> response) throws Exception {
+    GetConfigResponse getConfig(String policyName) throws Exception {
+
+        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( 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 @@
     }
 
     /**
+     * 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 {
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 b33550a..7ce03cc 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 @@
 
     @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 @@
 
         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 @@
 
         GetConfigRequest request = new GetConfigRequest();
         request.setPolicyName("policy");
-        policyFinder.makeOutboundCall(request, GetConfigResponse.class);
+        policyFinder.makeOutboundCall("",request, GetConfigResponse.class);
     }
 
     @Test
@@ -226,7 +228,7 @@
         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 @@
         policManProps.setUrl("http://policyManager.onap.org");
         GetConfigRequest request = new GetConfigRequest();
         request.setPolicyName("policy");
-        policyFinder.makeOutboundCall(request, GetConfigResponse.class);
+        policyFinder.makeOutboundCall("", request, GetConfigResponse.class);
     }
 
     @Test