4087e2ad2b59160d9d171919a657f5a1afbb3d3b
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2018 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.instar.node;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
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.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
45 import org.onap.sdnc.config.params.data.Parameter;
46
47 public class InstarClientNode implements SvcLogicJavaPlugin {
48
49     private static final EELFLogger log = EELFManager.getInstance().getLogger(InstarClientNode.class);
50
51
52     public void getInstarInfo(Map<String, String> inParams, SvcLogicContext ctx)
53         throws SvcLogicException {
54         log.info("Received getInstarInfo call with params : " + inParams);
55         String responsePrefix = inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
56         try {
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                 log.info("Received Context : " + ctx.getAttribute("INSTAR." + instarKey));
64                 Parameter params = mapper
65                     .readValue(ctx.getAttribute(InstarClientConstant.SOURCE_SYSTEM_INSTAR + "." + instarKey),
66                         Parameter.class);
67                 RuleHandlerInterface handler;
68                 log.info("Processing rule Type : " + params.getRuleType());
69                 if (params.getRuleType().equals(InstarClientConstant.INTERFACE_IP_ADDRESS)) {
70                     handler = createHandler(params, ctx);
71                 } else {
72                     throw new SvcLogicException("No Rule Defined to process :" + params.getRuleType());
73                 }
74                 handler.processRule();
75             }
76             log.info("responsePrefix =" + responsePrefix);
77             log.info("instar key values =" + ctx.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
78             ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES,
79                 ctx.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
80             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS,
81                 InstarClientConstant.OUTPUT_STATUS_SUCCESS);
82             log.info(ctx.getAttribute("TEST." + InstarClientConstant.OUTPUT_PARAM_STATUS));
83             ctx.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, null);
84         } catch (Exception e) {
85             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS,
86                 InstarClientConstant.OUTPUT_STATUS_FAILURE);
87             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
88             log.error("Failed processing Instar request", e);
89             throw new SvcLogicException(e.getMessage());
90         }
91     }
92
93     private static String[] getKeys(String keyString) {
94         log.error("Received Key String as :" + keyString);
95         String key = keyString
96             .replace("[", "")
97             .replace("]", "")
98             .replace("\"", "");
99         if (key.contains(",")) {
100             return key.split(",");
101         } else {
102             return new String[]{key};
103         }
104     }
105
106     public void getInstarData(Map<String, String> inParams, SvcLogicContext ctx)
107         throws SvcLogicException {
108         log.info("Received getInstarData call with params : " + inParams);
109         String responsePrefix = inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
110         try {
111             HashMap<String, String> input = new HashMap<>();
112             input.putAll(inParams);
113             RestClientInterface rcINterface = createRestClientInterface(input);
114             String response = rcINterface.sendRequest(inParams.get("operationName"));
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         } catch (Exception e) {
120             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS,
121                 InstarClientConstant.OUTPUT_STATUS_FAILURE);
122             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
123             log.error("Failed processing Instar request", e);
124             throw new SvcLogicException(e.getMessage());
125         }
126     }
127
128     public void getAaiInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
129         log.info("Received getAaiInfo call with params : " + inParams);
130         String responsePrefix = inParams.get(AaiClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
131         try {
132             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";
133             String[] aaiKeys = getKeys(inParams.get(AaiClientConstant.AAI_KEYS));
134             for (String aaiKey : aaiKeys) {
135                 log.info("Processing Key : " + aaiKey);
136                 log.info("Searching key for  : " + "AAI." + aaiKey);
137                 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
138                 log.info("Received Context : " + ctx.getAttribute("AAI." + aaiKey));
139                 Parameter params = mapper.readValue(
140                     ctx.getAttribute(AaiClientConstant.SOURCE_SYSTEM_AAI + "." + aaiKey), Parameter.class);
141                 log.info("Processing rule Type : " + params.getRuleType());
142                 RuleHandlerInterface handler = new AaiInterfaceRulesHandler(params, ctx);
143                 handler.processRule();
144             }
145             log.info("responsePrefix =" + responsePrefix);
146             ctx.setAttribute(responsePrefix + AaiClientConstant.AAI_KEY_VALUES,
147                 ctx.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
148             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_STATUS,
149                 AaiClientConstant.OUTPUT_STATUS_SUCCESS);
150             ctx.setAttribute(AaiClientConstant.AAI_KEY_VALUES, null);
151         } catch (Exception e) {
152             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_STATUS,
153                 InstarClientConstant.OUTPUT_STATUS_FAILURE);
154             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
155             log.error("Failed processing AAI data", e);
156             throw new SvcLogicException(e.getMessage());
157         }
158     }
159
160     protected RuleHandlerInterface createHandler(Parameter params, SvcLogicContext ctx) {
161         return new InterfaceIpAddressImpl(params, ctx);
162     }
163
164     protected RestClientInterface createRestClientInterface(Map<String, String> input) {
165         return new InstarRestClientImpl(input);
166     }
167 }