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