b9d97b4bfaa69354087feff211b5692955e08203
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.instar.interfaceImpl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.json.JSONArray;
30 import org.json.JSONObject;
31 import org.onap.appc.instar.interfaces.ResponseHandlerInterface;
32 import org.onap.appc.instar.utils.InstarClientConstant;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.sdnc.config.params.data.ResponseKey;
35
36 public class InstarResponseHandlerImpl implements ResponseHandlerInterface {
37
38     private static final EELFLogger log = EELFManager.getInstance().getLogger(InstarResponseHandlerImpl.class);
39
40     private ResponseKey resKey = null;
41     private SvcLogicContext ctxt = null;
42
43     public InstarResponseHandlerImpl(ResponseKey filterKeys, SvcLogicContext context) {
44         this.resKey = filterKeys;
45         this.ctxt = context;
46     }
47
48     @Override
49     public Object processResponse(String instarResponse, String instarKey) {
50         String fn = " InstarResponseHandlerImpl.processResponse ";
51         log.info(fn + " Instar Response :" + instarResponse);
52
53         JSONObject instarKeyValues;
54
55         log.info("Instar Data in Context : " + ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
56         if (ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES) != null) {
57             instarKeyValues = new JSONObject(ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
58             log.info("Instar data already exsits :  " + instarKeyValues.toString());
59         } else {
60             instarKeyValues = new JSONObject();
61         }
62         JSONArray instarResponses = new JSONObject(instarResponse)
63             .getJSONArray(InstarClientConstant.INSTAR_RESPONSE_BLOCK_NAME);
64         for (int i = 0; i < instarResponses.length(); i++) {
65             JSONObject res = instarResponses.getJSONObject(i);
66             log.info(fn + "Instar Block :" + i + " Values :" + res.toString());
67             log.info(fn + "Appc Filter Key :" + ctxt.getAttribute(InstarClientConstant.VNF_NAME) + resKey
68                 .getUniqueKeyValue());
69
70             if (hasValidFdqn(res)) {
71                 switch (resKey.getFieldKeyName()) {
72                     case InstarClientConstant.V4_ADDRESS:
73                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_ADDRESS));
74                         break;
75                     case InstarClientConstant.INSTAR_V4_SUBNET:
76                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_SUBNET));
77                         break;
78                     case InstarClientConstant.INSTAR_V4_DEFAULT_GATEWAY:
79                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_DEFAULT_GATEWAY));
80                         break;
81                     case InstarClientConstant.V6_ADDRESS:
82                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_ADDRESS));
83                         break;
84                     case InstarClientConstant.INSTAR_V6_SUBNET:
85                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_SUBNET));
86                         break;
87                     case InstarClientConstant.INSTAR_V6_DEFAULT_GATEWAY:
88                         instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_DEFAULT_GATEWAY));
89                         break;
90                     default:
91                         break;
92                 }
93                 break;
94             }
95         }
96         log.info(fn + "Instar KeyValues  :" + instarKeyValues);
97         ctxt.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, instarKeyValues.toString());
98
99         return instarKeyValues;
100     }
101
102     private boolean hasValidFdqn(JSONObject res) {
103         return res.getString(InstarClientConstant.FDQN) != null &&
104             res.getString(InstarClientConstant.FDQN)
105                 .equalsIgnoreCase(ctxt.getAttribute(InstarClientConstant.VNF_NAME) + resKey.getUniqueKeyValue());
106     }
107 }