08df6f02e4a71ba5868ff3cfc8d9a7cbad0b27a3
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / main / java / org / openecomp / appc / instar / interfaceImpl / InstarResponseHandlerImpl.java
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 org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.onap.appc.instar.interfaces.ResponseHandlerInterface;
30 import org.onap.appc.instar.utils.InstarClientConstant;
31 import org.openecomp.sdnc.config.params.data.ResponseKey;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 public class InstarResponseHandlerImpl implements ResponseHandlerInterface {
38
39         private static final EELFLogger log = EELFManager.getInstance().getLogger(InstarResponseHandlerImpl.class);
40
41         ResponseKey resKey = null;
42         SvcLogicContext ctxt = null;
43         
44         public InstarResponseHandlerImpl(ResponseKey filterKeys, SvcLogicContext context) {
45                 this.resKey = filterKeys;
46                 this.ctxt = context;
47                 
48         }
49
50         @Override
51         public Object processResponse(String instarResponse, String instarKey) {        
52                 String fn = " InstarResponseHandlerImpl.processResponse ";
53                 log.info(fn + " Instar Response :" + instarResponse);
54                 
55                 JSONObject instarKeyValues;
56                 
57                 log.info("Instar Data in Context : "+ ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
58                 if(ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES) != null){
59                         instarKeyValues = new JSONObject(ctxt.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
60                         log.info("Instar data already exsits :  " + instarKeyValues.toString());
61                 }
62                 else
63                         instarKeyValues = new JSONObject();
64                 JSONArray instarResponses = new JSONObject(instarResponse).getJSONArray(InstarClientConstant.INSTAR_RESPONSE_BLOCK_NAME);
65                 for (int i = 0; i < instarResponses.length(); i++){
66                         JSONObject res = instarResponses.getJSONObject(i);
67                         log.info(fn + "Instar Block :" + i + " Values :" +  res.toString());
68                         log.info(fn + "Appc Filter Key :"  +  ctxt.getAttribute(InstarClientConstant.VNF_NAME) + resKey.getUniqueKeyValue());
69                         
70                         if(res.getString(InstarClientConstant.FDQN) != null &&
71                                         res.getString(InstarClientConstant.FDQN).equalsIgnoreCase(ctxt.getAttribute(InstarClientConstant.VNF_NAME) + resKey.getUniqueKeyValue())){                                      
72                                         if(resKey.getFieldKeyName().equals(InstarClientConstant.V4_ADDRESS))
73                                                 instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V4_ADDRESS));
74                                         else if(resKey.getFieldKeyName().equals(InstarClientConstant.V6_ADDRESS))
75                                                 instarKeyValues.put(instarKey, res.getString(InstarClientConstant.INSTAR_V6_ADDRESS));                          
76                                         break;
77                         }
78                 }
79                 log.info(fn + "Instar KeyValues  :" + instarKeyValues);
80                 ctxt.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, instarKeyValues.toString());
81                 
82         
83                 return instarKeyValues;
84         }
85
86 }