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