Network Inventory Client Updates
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / main / java / org / onap / appc / instar / node / InstarClientNode.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.node;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
31
32 import java.util.HashMap;
33 import java.util.Map;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.appc.aai.interfaceImpl.AaiInterfaceRulesHandler;
36 import org.onap.appc.aai.utils.AaiClientConstant;
37 import org.onap.appc.instar.interfaceImpl.InstarRestClientImpl;
38 import org.onap.appc.instar.interfaceImpl.InterfaceIpAddressImpl;
39 import org.onap.appc.instar.interfaces.RestClientInterface;
40 import org.onap.appc.instar.interfaces.RuleHandlerInterface;
41 import org.onap.appc.instar.utils.InstarClientConstant;
42 import org.onap.sdnc.config.params.data.Parameter;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
45 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
46
47 public class InstarClientNode implements SvcLogicJavaPlugin
48 {
49     private static final EELFLogger log = EELFManager.getInstance().getLogger(InstarClientNode.class);
50
51     public void getInstarInfo(Map<String, String> inParams, SvcLogicContext ctx)
52     throws SvcLogicException{
53         log.info("Received getInstarInfo call with params : " + inParams);
54         String responsePrefix = (String)inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
55         try
56         {
57             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";
58             String [] instarKeys = getKeys(inParams.get(InstarClientConstant.INSTAR_KEYS));
59             for (String instarKey : instarKeys){
60                 log.info("Processing Key : " + instarKey);
61                 log.info("Searching key for  : " + "INSTAR." + instarKey);
62                 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
63                 RuleHandlerInterface handler = null;
64                 log.info("Received Context : " + ctx.getAttribute("INSTAR." + instarKey));
65                 Parameter params = mapper.readValue(ctx.getAttribute(InstarClientConstant.SOURCE_SYSTEM_INSTAR + "." +  instarKey), Parameter.class);
66
67                 log.info("Processing rule Type : "  + params.getRuleType());
68                 switch(params.getRuleType()){
69                     case InstarClientConstant.INTERFACE_IP_ADDRESS:
70                         handler = new InterfaceIpAddressImpl(params, ctx);
71                         break;
72                     default:
73                         throw new Exception("No Rule Defined to process :" + params.getRuleType());
74                 }
75                 handler.processRule();
76
77             }
78             log.info("responsePrefix =" + responsePrefix);
79             ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES, ctx.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
80             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_SUCCESS);
81             ctx.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, null);
82         }
83         catch (Exception e)
84         {
85             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_FAILURE);
86             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
87             log.error("Failed processing Instar request" + e.getMessage());
88             e.printStackTrace();
89             throw new SvcLogicException(e.getMessage());
90         }
91     }
92      private static String[] getKeys(String keyString) {
93         String fn = "InstarClientNode.getKeys";
94         System.out.println("Received Key String as :" + keyString);
95
96          keyString = keyString.replace("[","");
97          keyString = keyString.replace("]", "");
98         keyString = keyString.replace("\"", "");
99          if(keyString.contains(","))
100          {
101                  String[] keys  = keyString.split(",");
102                  return keys;
103          }
104          else{
105                  String[] keys = {keyString};
106                  return keys;
107          }
108     }
109     public void getInstarData(Map<String, String> inParams, SvcLogicContext ctx)
110             throws SvcLogicException{
111                 log.info("Received getInstarData call with params : " + inParams);
112                 String responsePrefix = (String)inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
113                 try
114                 {
115                     HashMap<String, String> input  = new HashMap<String, String>();
116                     input.putAll(inParams);
117                     RestClientInterface rcINterface = new InstarRestClientImpl(input);
118                     String response = rcINterface.sendRequest(inParams.get("operationName"));
119
120                     responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";
121                     ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_SUCCESS);
122                     ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES, response);
123
124                 }
125                 catch (Exception e)
126                 {
127                     ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_FAILURE);
128                     ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
129                     log.error("Failed processing Instar request" + e.getMessage());
130                     e.printStackTrace();
131                     throw new SvcLogicException(e.getMessage());
132                 }
133             }
134 public void getAaiInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
135         log.info("Received getAaiInfo call with params : " + inParams);
136         String responsePrefix = (String) inParams.get(AaiClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
137         try {
138             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";
139             String[] aaiKeys = getKeys(inParams.get(AaiClientConstant.AAI_KEYS));
140             for (String aaiKey : aaiKeys) {
141                 log.info("Processing Key : " + aaiKey);
142                 log.info("Searching key for  : " + "AAI." + aaiKey);
143                 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
144                 RuleHandlerInterface handler = null;
145                 log.info("Received Context : " + ctx.getAttribute("AAI." + aaiKey));
146                 Parameter params = mapper.readValue(
147                         ctx.getAttribute(AaiClientConstant.SOURCE_SYSTEM_AAI + "." + aaiKey), Parameter.class);
148                 log.info("Processing rule Type : " + params.getRuleType());
149                 handler = new AaiInterfaceRulesHandler(params, ctx);
150                 handler.processRule();
151
152             }
153
154             log.info("responsePrefix =" + responsePrefix);
155             ctx.setAttribute(responsePrefix + AaiClientConstant.AAI_KEY_VALUES,
156                     ctx.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
157             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_STATUS,
158                     AaiClientConstant.OUTPUT_STATUS_SUCCESS);
159             ctx.setAttribute(AaiClientConstant.AAI_KEY_VALUES, null);
160         } catch (Exception e) {
161             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_STATUS,
162                     InstarClientConstant.OUTPUT_STATUS_FAILURE);
163             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
164             log.error("Failed processing AAI data" + e.getMessage());
165             e.printStackTrace();
166             throw new SvcLogicException(e.getMessage());
167         }
168     }
169 }