Network Inventory Cient Platform Hardening
[appc.git] / appc-outbound / appc-network-inventory-client / provider / src / main / java / org / onap / appc / aai / interfaceImpl / AaiInterfaceRulesHandler.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.aai.interfaceImpl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.apache.commons.lang.StringUtils;
31 import org.json.JSONObject;
32 import org.onap.appc.aai.data.AaiVmInfo;
33 import org.onap.appc.aai.data.AaiVnfInfo;
34 import org.onap.appc.aai.data.AaiVnfcInfo;
35 import org.onap.appc.aai.utils.AaiClientConstant;
36 import org.onap.appc.system.interfaces.RuleHandlerInterface;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.onap.sdnc.config.params.data.Parameter;
39 import org.onap.sdnc.config.params.data.ResponseKey;
40
41 public class AaiInterfaceRulesHandler implements RuleHandlerInterface {
42
43     private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiInterfaceRulesHandler.class);
44     private static final String STR_RETURNING_VALUES = "Returning values: ";
45     private static final String STR_VNF_INFO_VM = "tmp.vnfInfo.vm[";
46
47     private Parameter parameters;
48     private SvcLogicContext context;
49     private AaiVnfInfo vnfInfoData;
50
51     public AaiInterfaceRulesHandler(Parameter params, SvcLogicContext ctx) {
52         this.parameters = params;
53         this.context = ctx;
54         this.setVnfInfoData(generateAaiVnfInfoData());
55     }
56
57     @Override
58     public void processRule() {
59
60         String fn = "AaiInterfaceIpAddressHandler.processRule";
61         log.info(fn + "Processing rule :" + parameters.getRuleType());
62         List<ResponseKey> responseKeyList = parameters.getResponseKeys();
63         ResponseKey respKeys = new ResponseKey();
64
65         if (responseKeyList == null || responseKeyList.isEmpty()) {
66             throw new IllegalStateException("NO response Keys set  for : " + parameters.getRuleType());
67         }
68
69         for (ResponseKey filterKeys : responseKeyList) {
70
71             if (null == filterKeys) {
72                 continue;
73             }
74             if (StringUtils.isNotBlank(filterKeys.getFieldKeyName())) {
75                 respKeys.setFieldKeyName(filterKeys.getFieldKeyName());
76             }
77             trySetUniqueKey(respKeys, filterKeys);
78             trySetFilters(respKeys, filterKeys);
79         }
80         processKeys(respKeys, parameters.getName());
81     }
82
83     private void trySetFilters(ResponseKey respKeys, ResponseKey filterKeys) {
84         if (StringUtils.isNotBlank(filterKeys.getFilterByField())) {
85             respKeys.setFilterByField(filterKeys.getFilterByField());
86         }
87         if (StringUtils.isNotBlank(filterKeys.getFilterByValue())) {
88             respKeys.setFilterByValue(filterKeys.getFilterByValue());
89         }
90     }
91
92     private void trySetUniqueKey(ResponseKey respKeys, ResponseKey filterKeys) {
93         if (StringUtils.isNotBlank(filterKeys.getUniqueKeyName())) {
94             respKeys.setUniqueKeyName(filterKeys.getUniqueKeyName());
95         }
96         if (StringUtils.isNotBlank(filterKeys.getUniqueKeyValue())) {
97             respKeys.setUniqueKeyValue(filterKeys.getUniqueKeyValue());
98         }
99     }
100
101     private void processKeys(ResponseKey filterKey, String aaiKey) {
102
103         String fn = "AaiInterfaceRulesHandler.processKeys()::";
104         log.info(fn + "processing for " + aaiKey);
105         String values = "";
106         JSONObject aaiKeyValues;
107         log.info("Aai Data in Context : " + context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
108         if (context.getAttribute(AaiClientConstant.AAI_KEY_VALUES) != null) {
109             aaiKeyValues = new JSONObject(context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
110             log.info("Aai data already exsits :  " + aaiKeyValues.toString());
111         } else {
112             aaiKeyValues = new JSONObject();
113         }
114
115         if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnf")) {
116             values = getVnfDetailsFromContext(filterKey.getFieldKeyName());
117
118         }
119         if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnfc")) {
120             values = getVnfcDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
121                 filterKey.getFilterByValue());
122         }
123         if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vserver")) {
124             values = getVServerDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
125                 filterKey.getFilterByValue());
126         }
127         aaiKeyValues.put(aaiKey, values);
128         context.setAttribute(AaiClientConstant.AAI_KEY_VALUES, aaiKeyValues.toString());
129     }
130
131     private String getVServerDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
132         String fn = "AaiInterfaceRulesHander::getVServerDetailsFromContext():";
133         StringBuilder values = new StringBuilder("");
134         log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByName:" + filterByField + " FilterByValue:"
135             + filterByValue);
136
137         if (!StringUtils.equalsIgnoreCase(fieldKeyName, "vserver-name")) {
138             log.info(fn + STR_RETURNING_VALUES + values);
139             return values.toString();
140         }
141
142         if (validateFilters(filterByField, filterByValue)) {
143             int vmIndex = -1;
144             for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
145                 vmIndex++;
146
147                 int vmNumber = Integer.parseInt(filterByValue);
148                 if (!StringUtils.equalsIgnoreCase(filterByField, "vm-number") || vmNumber != vmIndex) {
149                     continue;
150                 }
151                 if (StringUtils.isBlank(values.toString())) {
152                     values = new StringBuilder(vm.getVserverName());
153                 } else {
154                     values.append(",").append(vm.getVserverName());
155                 }
156             }
157         } else {
158             for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
159                 if (StringUtils.isBlank(values.toString())) {
160                     values = new StringBuilder(vm.getVserverName());
161                 } else {
162                     values.append(",").append(vm.getVserverName());
163                 }
164             }
165         }
166         log.info(fn + STR_RETURNING_VALUES + values);
167         return values.toString();
168     }
169
170     private boolean validateFilters(String filterByField, String filterByValue) {
171         return StringUtils.isNotEmpty(filterByField) && StringUtils.isNotEmpty(filterByValue);
172     }
173
174     //split from getVnfcDetailsFromContext
175     private String add2ValuesIpaddressV4OamVipNotEmpty(String values, String filterByField, String filterByValue) {
176         StringBuilder builder = new StringBuilder(values);
177         for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
178
179             for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
180                 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
181                     || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
182                     continue;
183                 }
184
185                 if (StringUtils.isBlank(builder.toString())) {
186                     builder = new StringBuilder(vnfcInfo.getVnfcOamIpAddress());
187                 } else {
188                     builder.append(",").append(vnfcInfo.getVnfcOamIpAddress());
189                 }
190             }
191         }
192         return builder.toString();
193     }
194
195     //split from getVnfcDetailsFromContext
196     private String add2ValuesIpaddressV4OamVipEmpty(String values, String filterByField, String filterByValue) {
197         StringBuilder builder = new StringBuilder(values);
198         for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
199             for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
200                 if (StringUtils.isBlank(builder.toString())) {
201                     builder = new StringBuilder(vnfcInfo.getVnfcOamIpAddress());
202                 } else {
203                     builder.append(",").append(vnfcInfo.getVnfcOamIpAddress());
204                 }
205             }
206         }
207         return builder.toString();
208     }
209
210     //split from getVnfcDetailsFromContext
211     private String add2ValuesVnfcNameNotEmpty(String values, String filterByField, String filterByValue) {
212         StringBuilder builder = new StringBuilder(values);
213         for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
214             for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
215                 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
216                     || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
217                     continue;
218                 }
219                 if (StringUtils.isBlank(builder.toString())) {
220                     builder = new StringBuilder(vnfcInfo.getVnfcName());
221                 } else {
222                     builder.append(",").append(vnfcInfo.getVnfcName());
223                 }
224             }
225         }
226         return builder.toString();
227     }
228
229     //split from getVnfcDetailsFromContext
230     private String add2ValuesVnfcNameEmpty(String values, String filterByField, String filterByValue) {
231         for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
232             StringBuilder builder = new StringBuilder(values);
233             for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
234                 if (StringUtils.isBlank(builder.toString())) {
235                     builder = new StringBuilder(vnfcInfo.getVnfcName());
236                 } else {
237                     builder.append(",").append(vnfcInfo.getVnfcName());
238                 }
239             }
240         }
241         return values;
242     }
243
244     private String getVnfcDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
245         String fn = "AaiInterfaceRulesHander::getVnfcDetailsFromContext()";
246         String values = "";
247         log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByField:" + filterByField + " FilterByValue:"
248             + filterByValue);
249         if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipaddress-v4-oam-vip")) {
250             if (validateFilters(filterByField, filterByValue)) {
251                 values = add2ValuesIpaddressV4OamVipNotEmpty(values, filterByField, filterByValue);
252             } else {
253                 values = add2ValuesIpaddressV4OamVipEmpty(values, filterByField, filterByValue);
254             }
255         }
256         if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnfc-name")) {
257             if (StringUtils.isNotBlank(filterByField) && StringUtils.isNotBlank(filterByValue)) {
258                 values = add2ValuesVnfcNameNotEmpty(values, filterByField, filterByValue);
259             } else {
260                 values = add2ValuesVnfcNameEmpty(values, filterByField, filterByValue);
261             }
262         }
263         log.info(fn + STR_RETURNING_VALUES + values);
264         return values;
265     }
266
267     private String getVnfDetailsFromContext(String fieldKeyName) {
268
269         log.info("getVnfDetailsFromContext::" + fieldKeyName);
270         String values = "";
271         if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnf-name")) {
272             values = context.getAttribute("tmp.vnfInfo.vnf.vnf-name");
273         }
274         if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipv4-oam-ipaddress")) {
275             values = context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address");
276         }
277         return values;
278     }
279
280     public AaiVnfInfo getVnfInfoData() {
281         return vnfInfoData;
282     }
283
284     private void setVnfInfoData(AaiVnfInfo vnfInfoData) {
285         this.vnfInfoData = vnfInfoData;
286     }
287
288     private AaiVnfInfo generateAaiVnfInfoData() {
289
290         log.info("AaiInterfaceRulesHandlerImpl:generateAaiVnfInfoData(): Printing variables in context");
291         for (Object key : context.getAttributeKeySet()) {
292             String parmName = (String) key;
293             String parmValue = context.getAttribute(parmName);
294             log.debug("generateAaiVnfInfoData():: " + parmName + "=" + parmValue);
295
296         }
297         String vmcount = context.getAttribute("tmp.vnfInfo.vm-count");
298         int vmCount = 0;
299         if (!StringUtils.isBlank(vmcount)) {
300             vmCount = Integer.parseInt(vmcount);
301         }
302         log.info("generateAaiVnfInfoData::" + "vmCount:" + vmCount);
303         AaiVnfInfo vnfInfo = new AaiVnfInfo();
304         vnfInfo.setVnfName("vnf-name");
305         ArrayList<AaiVmInfo> vmList = new ArrayList<>();
306
307         for (int i = 0; i < vmCount; i++) {
308             AaiVmInfo vm = new AaiVmInfo();
309             String vnfcCountStr = context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-count");
310             int vnfcCount = Integer.parseInt(vnfcCountStr);
311             ArrayList<AaiVnfcInfo> vnfcInfoList = new ArrayList<>();
312             for (int j = 0; j < vnfcCount; j++) {
313                 AaiVnfcInfo vnfcInfo = new AaiVnfcInfo();
314                 vnfcInfo.setVnfcName(context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-name"));
315                 vnfcInfo.setVnfcFunctionCode(context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-function-code"));
316                 vnfcInfo.setVnfcOamIpAddress(
317                     context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-ipaddress-v4-oam-vip"));
318                 vnfcInfoList.add(vnfcInfo);
319             }
320             vm.setVnfcInfo(vnfcInfoList);
321             vm.setVserverId(context.getAttribute(STR_VNF_INFO_VM + i + "].vserver-id"));
322             vm.setVserverName(context.getAttribute(STR_VNF_INFO_VM + i + "].vserver-name"));
323             vmList.add(vm);
324         }
325         vnfInfo.setVmInfo(vmList);
326         return vnfInfo;
327     }
328 }