Changed to unmaintained
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / main / java / org / onap / appc / instar / interfaceImpl / InstarResponseHandlerImpl.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  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.instar.interfaceImpl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.onap.appc.instar.interfaces.ResponseHandlerInterface;
31 import org.onap.appc.instar.utils.InstarClientConstant;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.sdnc.config.params.data.ResponseKey;
34
35 public class InstarResponseHandlerImpl implements ResponseHandlerInterface {
36
37     private static final EELFLogger log = EELFManager.getInstance().getLogger(InstarResponseHandlerImpl.class);
38
39     private ResponseKey resKey = null;
40     private SvcLogicContext ctxt = null;
41
42     public InstarResponseHandlerImpl(ResponseKey filterKeys, SvcLogicContext context) {
43         this.resKey = filterKeys;
44         this.ctxt = context;
45     }
46
47     @Override
48     public Object processResponse(String instarResponse, String instarKey) {
49         String fn = " InstarResponseHandlerImpl.processResponse ";
50         log.info(fn + " Instar Response :" + instarResponse);
51
52         JSONObject instarKeyValues;
53
54         log.info("Instar Data in Context : " + ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
55         if (ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES) != null) {
56             instarKeyValues = new JSONObject(ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
57             log.info("Instar data already exsits :  " + instarKeyValues.toString());
58         } else {
59             instarKeyValues = new JSONObject();
60         }
61         JSONArray instarResponses = new JSONObject(instarResponse)
62             .getJSONArray(InstarClientConstant.INSTAR_RESPONSE_BLOCK_NAME);
63         for (int i = 0; i < instarResponses.length(); i++) {
64             JSONObject res = instarResponses.getJSONObject(i);
65             log.info(fn + "Instar Block :" + i + " Values :" + res.toString());
66             log.info(fn + "Appc Filter Key :" + ctxt.getAttribute(InstarClientConstant.VNF_NAME) + resKey
67                 .getUniqueKeyValue());
68
69             if (hasValidFdqn(res)) {
70                 switch (resKey.getFieldKeyName()) {
71                     case InstarClientConstant.V4_ADDRESS:
72                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_ADDRESS));
73                         break;
74                     case InstarClientConstant.INSTAR_V4_SUBNET:
75                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_SUBNET));
76                         break;
77                     case InstarClientConstant.INSTAR_V4_DEFAULT_GATEWAY:
78                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_DEFAULT_GATEWAY));
79                         break;
80                     case InstarClientConstant.V6_ADDRESS:
81                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_ADDRESS));
82                         break;
83                     case InstarClientConstant.INSTAR_V6_SUBNET:
84                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_SUBNET));
85                         break;
86                     case InstarClientConstant.INSTAR_V6_DEFAULT_GATEWAY:
87                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_DEFAULT_GATEWAY));
88                         break;
89                     default:
90                         break;
91                 }
92                 break;
93             }
94         }
95         log.info(fn + "Instar KeyValues  :" + instarKeyValues);
96         ctxt.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, instarKeyValues.toString());
97
98         return instarKeyValues;
99     }
100
101     private boolean hasValidFdqn(JSONObject res) {
102         return res.getString(InstarClientConstant.FDQN) != null &&
103             res.getString(InstarClientConstant.FDQN)
104                 .equalsIgnoreCase(ctxt.getAttribute(InstarClientConstant.VNF_NAME) + resKey.getUniqueKeyValue());
105     }
106 }