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