797bbd5e7c3a72ee2de82803f67c5c19e0b6cb3c
[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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.instar.node;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.appc.aai.interfaceImpl.AaiInterfaceRulesHandler;
34 import org.onap.appc.aai.utils.AaiClientConstant;
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.ccsdk.sli.core.sli.SvcLogicContext;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
43 import org.onap.sdnc.config.params.data.Parameter;
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 = inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
53         try {
54             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";
55             String[] instarKeys = getKeys(inParams.get(InstarClientConstant.INSTAR_KEYS));
56             for (String instarKey : instarKeys) {
57                 log.info("Processing Key : " + instarKey);
58                 log.info("Searching key for  : " + "INSTAR." + instarKey);
59                 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
60                 RuleHandlerInterface handler;
61                 log.info("Received Context : " + ctx.getAttribute("INSTAR." + instarKey));
62                 Parameter params = mapper
63                     .readValue(ctx.getAttribute(InstarClientConstant.SOURCE_SYSTEM_INSTAR + "." + instarKey),
64                         Parameter.class);
65
66                 log.info("Processing rule Type : " + params.getRuleType());
67                 if (params.getRuleType().equals(InstarClientConstant.INTERFACE_IP_ADDRESS)) {
68                     handler = new InterfaceIpAddressImpl(params, ctx);
69                 } else {
70                     throw new SvcLogicException("No Rule Defined to process :" + params.getRuleType());
71                 }
72                 handler.processRule();
73             }
74             log.info("responsePrefix =" + responsePrefix);
75             ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES,
76                 ctx.getAttribute(InstarClientConstant.INSTAR_KEY_VALUES));
77             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS,
78                 InstarClientConstant.OUTPUT_STATUS_SUCCESS);
79             ctx.setAttribute(InstarClientConstant.INSTAR_KEY_VALUES, null);
80         } catch (Exception e) {
81             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS,
82                 InstarClientConstant.OUTPUT_STATUS_FAILURE);
83             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
84             log.error("Failed processing Instar request", e);
85             throw new SvcLogicException(e.getMessage());
86         }
87     }
88
89     private static String[] getKeys(String keyString) {
90         log.error("Received Key String as :" + keyString);
91
92         String key = keyString
93             .replace("[", "")
94             .replace("]", "")
95             .replace("\"", "");
96
97         if (key.contains(",")) {
98             return key.split(",");
99         } else {
100             return new String[]{key};
101         }
102     }
103
104     public void getInstarData(Map<String, String> inParams, SvcLogicContext ctx)
105         throws SvcLogicException {
106         log.info("Received getInstarData call with params : " + inParams);
107         String responsePrefix = inParams.get(InstarClientConstant.INPUT_PARAM_RESPONSE_PRIFIX);
108         try {
109             HashMap<String, String> input = new HashMap<>();
110             input.putAll(inParams);
111             RestClientInterface rcINterface = new InstarRestClientImpl(input);
112             String response = rcINterface.sendRequest(inParams.get("operationName"));
113
114             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? responsePrefix + "." : "";
115             ctx.setAttribute(responsePrefix + InstarClientConstant.OUTPUT_PARAM_STATUS,
116                 InstarClientConstant.OUTPUT_STATUS_SUCCESS);
117             ctx.setAttribute(responsePrefix + InstarClientConstant.INSTAR_KEY_VALUES, response);
118
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                 RuleHandlerInterface handler;
139                 log.info("Received Context : " + ctx.getAttribute("AAI." + aaiKey));
140                 Parameter params = mapper.readValue(
141                     ctx.getAttribute(AaiClientConstant.SOURCE_SYSTEM_AAI + "." + aaiKey), Parameter.class);
142                 log.info("Processing rule Type : " + params.getRuleType());
143                 handler = new AaiInterfaceRulesHandler(params, ctx);
144                 handler.processRule();
145             }
146             log.info("responsePrefix =" + responsePrefix);
147             ctx.setAttribute(responsePrefix + AaiClientConstant.AAI_KEY_VALUES,
148                 ctx.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
149             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_STATUS,
150                 AaiClientConstant.OUTPUT_STATUS_SUCCESS);
151             ctx.setAttribute(AaiClientConstant.AAI_KEY_VALUES, null);
152         } catch (Exception e) {
153             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_STATUS,
154                 InstarClientConstant.OUTPUT_STATUS_FAILURE);
155             ctx.setAttribute(responsePrefix + AaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
156             log.error("Failed processing AAI data", e);
157             throw new SvcLogicException(e.getMessage());
158         }
159     }
160 }