2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.aai.interfaceImpl;
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;
42 public class AaiInterfaceRulesHandler implements RuleHandlerInterface {
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[";
48 private Parameter parameters;
49 private SvcLogicContext context;
50 private AaiVnfInfo vnfInfoData;
52 public AaiInterfaceRulesHandler(Parameter params, SvcLogicContext ctx) {
53 this.parameters = params;
55 this.setVnfInfoData(generateAaiVnfInfoData());
59 public void processRule() {
61 String fn = "AaiInterfaceIpAddressHandler.processRule";
62 log.info(fn + "Processing rule :" + parameters.getRuleType());
63 List<ResponseKey> responseKeyList = parameters.getResponseKeys();
64 ResponseKey respKeys = new ResponseKey();
66 if (responseKeyList == null || responseKeyList.isEmpty()) {
67 throw new IllegalStateException("NO response Keys set for : " + parameters.getRuleType());
70 for (ResponseKey filterKeys : responseKeyList) {
72 if (null == filterKeys) {
75 if (StringUtils.isNotBlank(filterKeys.getFieldKeyName())) {
76 respKeys.setFieldKeyName(filterKeys.getFieldKeyName());
78 trySetUniqueKey(respKeys, filterKeys);
79 trySetFilters(respKeys, filterKeys);
81 processKeys(respKeys, parameters.getName());
84 private void trySetFilters(ResponseKey respKeys, ResponseKey filterKeys) {
85 if (StringUtils.isNotBlank(filterKeys.getFilterByField())) {
86 respKeys.setFilterByField(filterKeys.getFilterByField());
88 if (StringUtils.isNotBlank(filterKeys.getFilterByValue())) {
89 respKeys.setFilterByValue(filterKeys.getFilterByValue());
93 private void trySetUniqueKey(ResponseKey respKeys, ResponseKey filterKeys) {
94 if (StringUtils.isNotBlank(filterKeys.getUniqueKeyName())) {
95 respKeys.setUniqueKeyName(filterKeys.getUniqueKeyName());
97 if (StringUtils.isNotBlank(filterKeys.getUniqueKeyValue())) {
98 respKeys.setUniqueKeyValue(filterKeys.getUniqueKeyValue());
102 private void processKeys(ResponseKey filterKey, String aaiKey) {
104 String fn = "AaiInterfaceRulesHandler.processKeys()::";
105 log.info(fn + "processing for " + aaiKey);
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());
113 aaiKeyValues = new JSONObject();
116 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnf")) {
117 values = getVnfDetailsFromContext(filterKey.getFieldKeyName());
120 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnfc")) {
121 values = getVnfcDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
122 filterKey.getFilterByValue());
124 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vserver")) {
125 values = getVServerDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
126 filterKey.getFilterByValue());
128 aaiKeyValues.put(aaiKey, values);
129 context.setAttribute(AaiClientConstant.AAI_KEY_VALUES, aaiKeyValues.toString());
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:"
138 if (!StringUtils.equalsIgnoreCase(fieldKeyName, "vserver-name")) {
139 log.info(fn + STR_RETURNING_VALUES + values);
140 return values.toString();
143 if (validateFilters(filterByField, filterByValue)) {
145 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
148 int vmNumber = Integer.parseInt(filterByValue);
149 if (!StringUtils.equalsIgnoreCase(filterByField, "vm-number") || vmNumber != vmIndex) {
152 if (StringUtils.isBlank(values.toString())) {
153 values = new StringBuilder(vm.getVserverName());
155 values.append(",").append(vm.getVserverName());
159 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
160 if (StringUtils.isBlank(values.toString())) {
161 values = new StringBuilder(vm.getVserverName());
163 values.append(",").append(vm.getVserverName());
167 log.info(fn + STR_RETURNING_VALUES + values);
168 return values.toString();
171 private boolean validateFilters(String filterByField, String filterByValue) {
172 return StringUtils.isNotEmpty(filterByField) && StringUtils.isNotEmpty(filterByValue);
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()) {
180 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
181 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
182 || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
186 if (StringUtils.isBlank(builder.toString())) {
187 builder = new StringBuilder(vnfcInfo.getVnfcOamIpAddress());
189 builder.append(",").append(vnfcInfo.getVnfcOamIpAddress());
193 return builder.toString();
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());
204 builder.append(",").append(vnfcInfo.getVnfcOamIpAddress());
208 return builder.toString();
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())) {
220 if (StringUtils.isBlank(builder.toString())) {
221 builder = new StringBuilder(vnfcInfo.getVnfcName());
223 builder.append(",").append(vnfcInfo.getVnfcName());
227 return builder.toString();
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());
238 builder.append(",").append(vnfcInfo.getVnfcName());
245 private String getVnfcDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
246 String fn = "AaiInterfaceRulesHander::getVnfcDetailsFromContext()";
248 log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByField:" + filterByField + " FilterByValue:"
250 if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipaddress-v4-oam-vip")) {
251 if (validateFilters(filterByField, filterByValue)) {
252 values = add2ValuesIpaddressV4OamVipNotEmpty(values, filterByField, filterByValue);
254 values = add2ValuesIpaddressV4OamVipEmpty(values, filterByField, filterByValue);
257 if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnfc-name")) {
258 if (StringUtils.isNotBlank(filterByField) && StringUtils.isNotBlank(filterByValue)) {
259 values = add2ValuesVnfcNameNotEmpty(values, filterByField, filterByValue);
261 values = add2ValuesVnfcNameEmpty(values, filterByField, filterByValue);
264 log.info(fn + STR_RETURNING_VALUES + values);
268 private String getVnfDetailsFromContext(String fieldKeyName) {
270 log.info("getVnfDetailsFromContext::" + fieldKeyName);
272 if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnf-name")) {
273 values = context.getAttribute("tmp.vnfInfo.vnf.vnf-name");
275 if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipv4-oam-ipaddress")) {
276 values = context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address");
281 public AaiVnfInfo getVnfInfoData() {
285 private void setVnfInfoData(AaiVnfInfo vnfInfoData) {
286 this.vnfInfoData = vnfInfoData;
289 private AaiVnfInfo generateAaiVnfInfoData() {
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);
298 String vmcount = context.getAttribute("tmp.vnfInfo.vm-count");
300 if (!StringUtils.isBlank(vmcount)) {
301 vmCount = Integer.parseInt(vmcount);
303 log.info("generateAaiVnfInfoData::" + "vmCount:" + vmCount);
304 AaiVnfInfo vnfInfo = new AaiVnfInfo();
305 vnfInfo.setVnfName("vnf-name");
306 ArrayList<AaiVmInfo> vmList = new ArrayList<>();
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);
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"));
326 vnfInfo.setVmInfo(vmList);