2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.aai.interfaceImpl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.util.ArrayList;
29 import java.util.List;
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.aai.utils.AaiClientConstant;
36 import org.onap.appc.system.interfaces.RuleHandlerInterface;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 import org.onap.sdnc.config.params.data.Parameter;
39 import org.onap.sdnc.config.params.data.ResponseKey;
41 public class AaiInterfaceRulesHandler implements RuleHandlerInterface {
43 private static final EELFLogger log = EELFManager.getInstance().getLogger(AaiInterfaceRulesHandler.class);
44 private static final String STR_RETURNING_VALUES = "Returning values: ";
45 private static final String STR_VNF_INFO_VM = "tmp.vnfInfo.vm[";
47 private Parameter parameters;
48 private SvcLogicContext context;
49 private AaiVnfInfo vnfInfoData;
51 public AaiInterfaceRulesHandler(Parameter params, SvcLogicContext ctx) {
52 this.parameters = params;
54 this.setVnfInfoData(generateAaiVnfInfoData());
58 public void processRule() {
60 String fn = "AaiInterfaceIpAddressHandler.processRule";
61 log.info(fn + "Processing rule :" + parameters.getRuleType());
62 List<ResponseKey> responseKeyList = parameters.getResponseKeys();
63 ResponseKey respKeys = new ResponseKey();
65 if (responseKeyList == null || responseKeyList.isEmpty()) {
66 throw new IllegalStateException("NO response Keys set for : " + parameters.getRuleType());
69 for (ResponseKey filterKeys : responseKeyList) {
71 if (null == filterKeys) {
74 if (StringUtils.isNotBlank(filterKeys.getFieldKeyName())) {
75 respKeys.setFieldKeyName(filterKeys.getFieldKeyName());
77 trySetUniqueKey(respKeys, filterKeys);
78 trySetFilters(respKeys, filterKeys);
80 processKeys(respKeys, parameters.getName());
83 private void trySetFilters(ResponseKey respKeys, ResponseKey filterKeys) {
84 if (StringUtils.isNotBlank(filterKeys.getFilterByField())) {
85 respKeys.setFilterByField(filterKeys.getFilterByField());
87 if (StringUtils.isNotBlank(filterKeys.getFilterByValue())) {
88 respKeys.setFilterByValue(filterKeys.getFilterByValue());
92 private void trySetUniqueKey(ResponseKey respKeys, ResponseKey filterKeys) {
93 if (StringUtils.isNotBlank(filterKeys.getUniqueKeyName())) {
94 respKeys.setUniqueKeyName(filterKeys.getUniqueKeyName());
96 if (StringUtils.isNotBlank(filterKeys.getUniqueKeyValue())) {
97 respKeys.setUniqueKeyValue(filterKeys.getUniqueKeyValue());
101 private void processKeys(ResponseKey filterKey, String aaiKey) {
103 String fn = "AaiInterfaceRulesHandler.processKeys()::";
104 log.info(fn + "processing for " + aaiKey);
106 JSONObject aaiKeyValues;
107 log.info("Aai Data in Context : " + context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
108 if (context.getAttribute(AaiClientConstant.AAI_KEY_VALUES) != null) {
109 aaiKeyValues = new JSONObject(context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
110 log.info("Aai data already exsits : " + aaiKeyValues.toString());
112 aaiKeyValues = new JSONObject();
115 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnf")) {
116 values = getVnfDetailsFromContext(filterKey.getFieldKeyName());
119 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnfc")) {
120 values = getVnfcDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
121 filterKey.getFilterByValue());
123 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vserver")) {
124 values = getVServerDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
125 filterKey.getFilterByValue());
127 aaiKeyValues.put(aaiKey, values);
128 context.setAttribute(AaiClientConstant.AAI_KEY_VALUES, aaiKeyValues.toString());
131 private String getVServerDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
132 String fn = "AaiInterfaceRulesHander::getVServerDetailsFromContext():";
133 StringBuilder values = new StringBuilder("");
134 log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByName:" + filterByField + " FilterByValue:"
137 if (!StringUtils.equalsIgnoreCase(fieldKeyName, "vserver-name")) {
138 log.info(fn + STR_RETURNING_VALUES + values);
139 return values.toString();
142 if (validateFilters(filterByField, filterByValue)) {
144 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
147 int vmNumber = Integer.parseInt(filterByValue);
148 if (!StringUtils.equalsIgnoreCase(filterByField, "vm-number") || vmNumber != vmIndex) {
151 if (StringUtils.isBlank(values.toString())) {
152 values = new StringBuilder(vm.getVserverName());
154 values.append(",").append(vm.getVserverName());
158 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
159 if (StringUtils.isBlank(values.toString())) {
160 values = new StringBuilder(vm.getVserverName());
162 values.append(",").append(vm.getVserverName());
166 log.info(fn + STR_RETURNING_VALUES + values);
167 return values.toString();
170 private boolean validateFilters(String filterByField, String filterByValue) {
171 return StringUtils.isNotEmpty(filterByField) && StringUtils.isNotEmpty(filterByValue);
174 //split from getVnfcDetailsFromContext
175 private String add2ValuesIpaddressV4OamVipNotEmpty(String values, String filterByField, String filterByValue) {
176 StringBuilder builder = new StringBuilder(values);
177 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
179 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
180 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
181 || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
185 if (StringUtils.isBlank(builder.toString())) {
186 builder = new StringBuilder(vnfcInfo.getVnfcOamIpAddress());
188 builder.append(",").append(vnfcInfo.getVnfcOamIpAddress());
192 return builder.toString();
195 //split from getVnfcDetailsFromContext
196 private String add2ValuesIpaddressV4OamVipEmpty(String values, String filterByField, String filterByValue) {
197 StringBuilder builder = new StringBuilder(values);
198 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
199 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
200 if (StringUtils.isBlank(builder.toString())) {
201 builder = new StringBuilder(vnfcInfo.getVnfcOamIpAddress());
203 builder.append(",").append(vnfcInfo.getVnfcOamIpAddress());
207 return builder.toString();
210 //split from getVnfcDetailsFromContext
211 private String add2ValuesVnfcNameNotEmpty(String values, String filterByField, String filterByValue) {
212 StringBuilder builder = new StringBuilder(values);
213 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
214 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
215 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
216 || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
219 if (StringUtils.isBlank(builder.toString())) {
220 builder = new StringBuilder(vnfcInfo.getVnfcName());
222 builder.append(",").append(vnfcInfo.getVnfcName());
226 return builder.toString();
229 //split from getVnfcDetailsFromContext
230 private String add2ValuesVnfcNameEmpty(String values, String filterByField, String filterByValue) {
231 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
232 StringBuilder builder = new StringBuilder(values);
233 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
234 if (StringUtils.isBlank(builder.toString())) {
235 builder = new StringBuilder(vnfcInfo.getVnfcName());
237 builder.append(",").append(vnfcInfo.getVnfcName());
244 private String getVnfcDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
245 String fn = "AaiInterfaceRulesHander::getVnfcDetailsFromContext()";
247 log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByField:" + filterByField + " FilterByValue:"
249 if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipaddress-v4-oam-vip")) {
250 if (validateFilters(filterByField, filterByValue)) {
251 values = add2ValuesIpaddressV4OamVipNotEmpty(values, filterByField, filterByValue);
253 values = add2ValuesIpaddressV4OamVipEmpty(values, filterByField, filterByValue);
256 if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnfc-name")) {
257 if (StringUtils.isNotBlank(filterByField) && StringUtils.isNotBlank(filterByValue)) {
258 values = add2ValuesVnfcNameNotEmpty(values, filterByField, filterByValue);
260 values = add2ValuesVnfcNameEmpty(values, filterByField, filterByValue);
263 log.info(fn + STR_RETURNING_VALUES + values);
267 private String getVnfDetailsFromContext(String fieldKeyName) {
269 log.info("getVnfDetailsFromContext::" + fieldKeyName);
271 if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnf-name")) {
272 values = context.getAttribute("tmp.vnfInfo.vnf.vnf-name");
274 if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipv4-oam-ipaddress")) {
275 values = context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address");
280 public AaiVnfInfo getVnfInfoData() {
284 private void setVnfInfoData(AaiVnfInfo vnfInfoData) {
285 this.vnfInfoData = vnfInfoData;
288 private AaiVnfInfo generateAaiVnfInfoData() {
290 log.info("AaiInterfaceRulesHandlerImpl:generateAaiVnfInfoData(): Printing variables in context");
291 for (Object key : context.getAttributeKeySet()) {
292 String parmName = (String) key;
293 String parmValue = context.getAttribute(parmName);
294 log.debug("generateAaiVnfInfoData():: " + parmName + "=" + parmValue);
297 String vmcount = context.getAttribute("tmp.vnfInfo.vm-count");
299 if (!StringUtils.isBlank(vmcount)) {
300 vmCount = Integer.parseInt(vmcount);
302 log.info("generateAaiVnfInfoData::" + "vmCount:" + vmCount);
303 AaiVnfInfo vnfInfo = new AaiVnfInfo();
304 vnfInfo.setVnfName("vnf-name");
305 ArrayList<AaiVmInfo> vmList = new ArrayList<>();
307 for (int i = 0; i < vmCount; i++) {
308 AaiVmInfo vm = new AaiVmInfo();
309 String vnfcCountStr = context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-count");
310 int vnfcCount = Integer.parseInt(vnfcCountStr);
311 ArrayList<AaiVnfcInfo> vnfcInfoList = new ArrayList<>();
312 for (int j = 0; j < vnfcCount; j++) {
313 AaiVnfcInfo vnfcInfo = new AaiVnfcInfo();
314 vnfcInfo.setVnfcName(context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-name"));
315 vnfcInfo.setVnfcFunctionCode(context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-function-code"));
316 vnfcInfo.setVnfcOamIpAddress(
317 context.getAttribute(STR_VNF_INFO_VM + i + "].vnfc-ipaddress-v4-oam-vip"));
318 vnfcInfoList.add(vnfcInfo);
320 vm.setVnfcInfo(vnfcInfoList);
321 vm.setVserverId(context.getAttribute(STR_VNF_INFO_VM + i + "].vserver-id"));
322 vm.setVserverName(context.getAttribute(STR_VNF_INFO_VM + i + "].vserver-name"));
325 vnfInfo.setVmInfo(vmList);