First part of onap rename
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / main / java / org / openecomp / 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.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import org.apache.commons.lang3.StringUtils;
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.openecomp.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 = getInstarKeys(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[] getInstarKeys(String keyString) {
93          String fn = "InstarClientNode.getInstarKeys";
94          System.out.println("Received instar 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
135 }