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