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