54cc12fd7b7efe8b7f18161f4d52dffe27aa5e53
[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.instar.interfaceImpl.InstarRestClientImpl;
36 import org.onap.appc.instar.interfaceImpl.InterfaceIpAddressImpl;
37 import org.onap.appc.instar.interfaces.RestClientInterface;
38 import org.onap.appc.instar.interfaces.RuleHandlerInterface;
39 import org.onap.appc.instar.utils.InstarClientConstant;
40 import org.onap.sdnc.config.params.data.Parameter;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
44
45 public class InstarClientNode implements SvcLogicJavaPlugin
46 {
47         private static final EELFLogger log = EELFManager.getInstance().getLogger(InstarClientNode.class);
48
49         public void getInstarInfo(Map<String, String> inParams, SvcLogicContext ctx)
50         throws SvcLogicException{
51                 log.info("Received getInstarInfo call with params : " + inParams);
52                 String responsePrefix = (String)inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
53                 try
54                 {
55                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";                    
56                         String [] instarKeys = getInstarKeys(inParams.get(InstarClientConstant.INSTAR_KEYS));   
57                         for (String instarKey : instarKeys){
58                                 log.info("Processing Key : " + instarKey);
59                                 log.info("Searching key for  : " + "INSTAR." + instarKey);
60                                 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
61                                 RuleHandlerInterface handler = null;
62                                 log.info("Received Context : " + ctx.getAttribute("INSTAR." + instarKey));
63                                 Parameter params = mapper.readValue(ctx.getAttribute(InstarClientConstant.SOURCE_SYSTEM_INSTAR + "." +  instarKey), Parameter.class);
64                         
65                                 log.info("Processing rule Type : "  + params.getRuleType());    
66                                 switch(params.getRuleType()){                                   
67                                         case InstarClientConstant.INTERFACE_IP_ADDRESS:
68                                                 handler = new InterfaceIpAddressImpl(params, ctx);                      
69                                                 break;
70                                         default:
71                                                 throw new Exception("No Rule Defined to process :" + params.getRuleType());                     
72                                 }
73                                 handler.processRule();  
74                                 
75                         }
76                         log.info("responsePrefix =" + responsePrefix);
77                         ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES, ctx.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
78                         ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_SUCCESS);
79                         ctx.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, null);
80                 }
81                 catch (Exception e)
82                 {
83                         ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_FAILURE);
84                         ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
85                         log.error("Failed processing Instar request" + e.getMessage());
86                         e.printStackTrace();
87                         throw new SvcLogicException(e.getMessage());
88                 }
89         }
90          private static String[] getInstarKeys(String keyString) {
91          String fn = "InstarClientNode.getInstarKeys";
92          System.out.println("Received instar Key String as :" + keyString);
93
94          keyString = keyString.replace("[","");
95          keyString = keyString.replace("]", "");
96         keyString = keyString.replace("\"", "");
97          if(keyString.contains(","))
98          {
99                  String[] keys  = keyString.split(",");
100                  return keys;
101          }
102          else{
103                  String[] keys = {keyString};
104                  return keys;
105          }
106         }
107         public void getInstarData(Map<String, String> inParams, SvcLogicContext ctx)
108                         throws SvcLogicException{
109                                 log.info("Received getInstarData call with params : " + inParams);
110                                 String responsePrefix = (String)inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
111                                 try
112                                 {
113                                         HashMap<String, String> input  = new HashMap<String, String>();
114                                         input.putAll(inParams);
115                                         RestClientInterface rcINterface = new InstarRestClientImpl(input);
116                                         String response = rcINterface.sendRequest(inParams.get("operationName"));
117                                         
118                                         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";    
119                                         ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_SUCCESS);
120                                         ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES, response);
121                                                                                                 
122                                 }
123                                 catch (Exception e)
124                                 {
125                                         ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS, InstarClientConstant.OUTPUT_STATUS_FAILURE);
126                                         ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
127                                         log.error("Failed processing Instar request" + e.getMessage());
128                                         e.printStackTrace();
129                                         throw new SvcLogicException(e.getMessage());
130                                 }
131                         }
132
133 }