4340acca9c1e4b96907f487b5ff24780a46af44d
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / xinterface / XResponseProcessor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.design.xinterface;
27
28 import java.util.HashMap;
29 import org.onap.appc.design.services.util.DesignServiceConstants;
30 import org.onap.appc.instar.dme2client.Dme2Client;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import com.fasterxml.jackson.databind.JsonNode;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class XResponseProcessor {
37
38   private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class);
39   Dme2Client dme2Client;
40
41   public static XResponseProcessor getInstance() {
42     return new XResponseProcessor();
43   }
44
45   public String parseResponse(String execute, String action) throws Exception {
46     ObjectMapper objectMapper = new ObjectMapper();
47     JsonNode payloadObject = objectMapper.readTree(execute);
48     log.info("payloadObject " + payloadObject);
49
50     // String queryParam = null;
51     String instarResponse = null;
52     HashMap<String, String> payload = null;
53     String ipAddress = null;
54     String mask = null;
55
56     try {
57
58       // check the payload whether its having ipaddr along with subnet
59       ipAddress = payloadObject.get(DesignServiceConstants.INSTAR_V4_ADDRESS) != null
60           ? payloadObject.get(DesignServiceConstants.INSTAR_V4_ADDRESS).textValue()
61           : (payloadObject.get(DesignServiceConstants.INSTAR_V6_ADDRESS) != null)
62               ? payloadObject.get(DesignServiceConstants.INSTAR_V6_ADDRESS).textValue()
63                   .toUpperCase()
64               : null;
65
66       mask = payloadObject.get(DesignServiceConstants.INSTAR_V4_MASK) != null
67           ? payloadObject.get(DesignServiceConstants.INSTAR_V4_MASK).textValue()
68           : (payloadObject.get(DesignServiceConstants.INSTAR_V6_MASK) != null)
69               ? payloadObject.get(DesignServiceConstants.INSTAR_V6_MASK).textValue().toUpperCase()
70               : null;
71
72       // TODO -short format
73
74       /*
75        * if (mask != null) { queryParam = ipAddress + "," +mask ;
76        * log.info("Calling Instar with IpAddress "+ ipAddress + " Mask value: "+ mask ); } else {
77        * queryParam = "ipAddress "+ipAddress ; log.info("Calling Instar with IpAddress "+
78        * ipAddress); }
79        */
80
81       payload = new HashMap<String, String>();
82       payload.put("ipAddress", ipAddress);
83       payload.put("mask", mask);
84       log.info("Calling Instar with IpAddress " + ipAddress + " Mask value: " + mask);
85       dme2Client = new Dme2Client("getVnfbyIpadress", "payload", payload);
86
87       instarResponse = dme2Client.send();
88
89       log.debug("Resposne from Instar = " + instarResponse);
90       if (instarResponse == null || instarResponse.length() < 0)
91         throw new Exception("No Data received from Instar for this action " + action);
92     } catch (Exception e) {
93       e.printStackTrace();
94       throw e;
95     }
96     return instarResponse;
97   }
98 }