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