Network Inventory Client Updates
[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 java.util.ArrayList;
28 import java.util.List;
29
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.instar.interfaces.RuleHandlerInterface;
36 import org.onap.appc.aai.utils.AaiClientConstant;
37 import org.onap.sdnc.config.params.data.Parameter;
38 import org.onap.sdnc.config.params.data.ResponseKey;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43
44 public class AaiInterfaceRulesHandler implements RuleHandlerInterface {
45
46     private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiInterfaceRulesHandler.class);
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() throws Exception {
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         if (responseKeyList != null && responseKeyList.size() > 0) {
65             for (ResponseKey filterKeys : responseKeyList) {
66                 if (null != filterKeys) {
67                     if (StringUtils.isNotBlank(filterKeys.getUniqueKeyName()))
68                         respKeys.setUniqueKeyName(filterKeys.getUniqueKeyName());
69                     if (StringUtils.isNotBlank(filterKeys.getUniqueKeyValue()))
70                         respKeys.setUniqueKeyValue(filterKeys.getUniqueKeyValue());
71                     if (StringUtils.isNotBlank(filterKeys.getFieldKeyName()))
72                         respKeys.setFieldKeyName(filterKeys.getFieldKeyName());
73                     if (StringUtils.isNotBlank(filterKeys.getFilterByField()))
74                         respKeys.setFilterByField(filterKeys.getFilterByField());
75                     if (StringUtils.isNotBlank(filterKeys.getFilterByValue()))
76                         respKeys.setFilterByValue(filterKeys.getFilterByValue());
77                 }
78             }
79         } else {
80             throw new Exception("NO response Keys set  for : " + parameters.getRuleType());
81         }
82         processKeys(respKeys, parameters.getName());
83     }
84
85     public void processKeys(ResponseKey filterKey, String aaiKey) throws Exception {
86
87         String fn = "AaiInterfaceRulesHandler.processKeys()::";
88         log.info(fn + "processing for " + aaiKey);
89         String values = new String();
90         JSONObject aaiKeyValues = null;
91         log.info("Aai Data in Context : " + context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
92         if (context.getAttribute(AaiClientConstant.AAI_KEY_VALUES) != null) {
93             aaiKeyValues = new JSONObject(context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
94             log.info("Aai data already exsits :  " + aaiKeyValues.toString());
95         } else {
96             aaiKeyValues = new JSONObject();
97         }
98
99         if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnf")) {
100             values = getVnfDetailsFromContext(filterKey.getFieldKeyName());
101
102         }
103         if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnfc")) {
104             values = getVnfcDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
105                     filterKey.getFilterByValue());
106         }
107         if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vserver")) {
108             values = getVServerDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
109                     filterKey.getFilterByValue());
110         }
111         aaiKeyValues.put(aaiKey, values);
112         context.setAttribute(AaiClientConstant.AAI_KEY_VALUES, aaiKeyValues.toString());
113         return;
114     }
115
116     private String getVServerDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
117         String fn = "AaiInterfaceRulesHander::getVServerDetailsFromContext():";
118         String values = "";
119         log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByName:" + filterByField + " FilterByValue:"
120                 + filterByValue);
121         if (StringUtils.equalsIgnoreCase(fieldKeyName, "vserver-name")) {
122             if (StringUtils.isNotEmpty(filterByField) && StringUtils.isNotEmpty(filterByField)
123                     && StringUtils.isNotEmpty(filterByValue) && StringUtils.isNotEmpty(filterByValue)) {
124                 int vmIndex = 0;
125                 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
126                     if (StringUtils.equalsIgnoreCase(filterByField, "vm-number") && null != filterByValue) {
127                         int vmNumber = Integer.parseInt(filterByValue);
128                         if (vmNumber == vmIndex) {
129                             if (StringUtils.isBlank(values))
130                                 values = vm.getVserverName();
131                             else
132                                 values = values + "," + vm.getVserverName();
133                         }
134                     }
135                     vmIndex++;
136                 }
137             } else {
138                 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
139                     if (StringUtils.isBlank(values))
140                         values = vm.getVserverName();
141                     else
142                         values = values + "," + vm.getVserverName();
143                 }
144             }
145         }
146         log.info(fn + "Returning values:" + values);
147         return values;
148
149     }
150
151     private String getVnfcDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
152         String fn = "AaiInterfaceRulesHander::getVnfcDetailsFromContext()";
153         String values = "";
154         log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByField:" + filterByField + " FilterByValue:"
155                 + filterByValue);
156         if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipaddress-v4-oam-vip")) {
157             if (StringUtils.isNotEmpty(filterByField) && StringUtils.isNotEmpty(filterByValue)) {
158                 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
159                     for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
160                         if (StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
161                                 && StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode()))
162                             if (StringUtils.isBlank(values))
163                                 values = vnfcInfo.getVnfcOamIpAddress();
164                             else
165                                 values = values + "," + vnfcInfo.getVnfcOamIpAddress();
166                     }
167
168                 }
169             } else {
170                 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
171                     for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
172                         if (StringUtils.isBlank(values))
173                             values = vnfcInfo.getVnfcOamIpAddress();
174                         else
175                             values = values + "," + vnfcInfo.getVnfcOamIpAddress();
176                     }
177                 }
178
179             }
180         }
181         if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnfc-name")) {
182             if (StringUtils.isNotBlank(filterByField) && StringUtils.isNotBlank(filterByValue)) {
183                 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
184                     for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
185                         if (StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
186                                 && StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode()))
187                             if (StringUtils.isBlank(values))
188                                 values = vnfcInfo.getVnfcName();
189                             else
190                                 values = values + "," + vnfcInfo.getVnfcName();
191                     }
192
193                 }
194             } else {
195                 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
196                     for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
197                         if (StringUtils.isBlank(values))
198                             values = vnfcInfo.getVnfcName();
199                         else
200                             values = values + "," + vnfcInfo.getVnfcName();
201                     }
202                 }
203
204             }
205         }
206         log.info(fn + "Returning values:" + values);
207         return values;
208
209     }
210
211     private String getVnfDetailsFromContext(String fieldKeyName) {
212
213         log.info("getVnfDetailsFromContext::" + fieldKeyName);
214         String values = "";
215         if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnf-name")) {
216             String vnfName = context.getAttribute("tmp.vnfInfo.vnf.vnf-name");
217             values = vnfName;
218         }
219         if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipv4-oam-ipaddress")) {
220             String ipv4OamAddress = context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address");
221             values = ipv4OamAddress;
222         }
223         return values;
224     }
225
226     public AaiVnfInfo getVnfInfoData() {
227         return vnfInfoData;
228     }
229
230     public void setVnfInfoData(AaiVnfInfo vnfInfoData) {
231         this.vnfInfoData = vnfInfoData;
232     }
233
234     public AaiVnfInfo generateAaiVnfInfoData() {
235
236         log.info("AaiInterfaceRulesHandlerImpl:generateAaiVnfInfoData(): Printing variables in context");
237         for (Object key : context.getAttributeKeySet()) {
238             String parmName = (String) key;
239             String parmValue = context.getAttribute(parmName);
240             log.debug("generateAaiVnfInfoData():: " + parmName + "=" + parmValue);
241
242         }
243
244         String vmcount = context.getAttribute("tmp.vnfInfo.vm-count");
245         int vmCount = 0;
246         if (!StringUtils.isBlank(vmcount))
247             vmCount = Integer.parseInt(vmcount);
248         log.info("generateAaiVnfInfoData::" + "vmCount:" + vmCount);
249         AaiVnfInfo vnfInfo = new AaiVnfInfo();
250         vnfInfo.setVnfName("vnf-name");
251         ArrayList<AaiVmInfo> vmList = new ArrayList<AaiVmInfo>();
252
253         for (int i = 0; i < vmCount; i++) {
254             AaiVmInfo vm = new AaiVmInfo();
255             String vnfcCountStr = context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-count");
256             int vnfcCount = Integer.parseInt(vnfcCountStr);
257             ArrayList<AaiVnfcInfo> vnfcInfoList = new ArrayList<AaiVnfcInfo>();
258             for (int j = 0; j < vnfcCount; j++) {
259                 AaiVnfcInfo vnfcInfo = new AaiVnfcInfo();
260                 vnfcInfo.setVnfcName(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-name"));
261                 vnfcInfo.setVnfcFunctionCode(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-function-code"));
262                 vnfcInfo.setVnfcOamIpAddress(
263                         context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-ipaddress-v4-oam-vip"));
264                 vnfcInfoList.add(vnfcInfo);
265             }
266             vm.setVnfcInfo(vnfcInfoList);
267             vm.setVserverId(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-id"));
268             vm.setVserverName(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-name"));
269             vmList.add(vm);
270         }
271         vnfInfo.setVmInfo(vmList);
272         return vnfInfo;
273     }
274
275 }