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 Parameter parameters;
46 private SvcLogicContext context;
47 private AaiVnfInfo vnfInfoData;
49 public AaiInterfaceRulesHandler(Parameter params, SvcLogicContext ctx) {
50 this.parameters = params;
52 this.setVnfInfoData(generateAaiVnfInfoData());
56 public void processRule() throws IllegalStateException {
58 String fn = "AaiInterfaceIpAddressHandler.processRule";
59 log.info(fn + "Processing rule :" + parameters.getRuleType());
60 List<ResponseKey> responseKeyList = parameters.getResponseKeys();
61 ResponseKey respKeys = new ResponseKey();
63 if (responseKeyList == null || responseKeyList.isEmpty()) {
64 throw new IllegalStateException("NO response Keys set for : " + parameters.getRuleType());
67 for (ResponseKey filterKeys : responseKeyList) {
69 if (null == filterKeys) {
73 if (StringUtils.isNotBlank(filterKeys.getUniqueKeyName())) {
74 respKeys.setUniqueKeyName(filterKeys.getUniqueKeyName());
76 if (StringUtils.isNotBlank(filterKeys.getUniqueKeyValue())) {
77 respKeys.setUniqueKeyValue(filterKeys.getUniqueKeyValue());
79 if (StringUtils.isNotBlank(filterKeys.getFieldKeyName())) {
80 respKeys.setFieldKeyName(filterKeys.getFieldKeyName());
82 if (StringUtils.isNotBlank(filterKeys.getFilterByField())) {
83 respKeys.setFilterByField(filterKeys.getFilterByField());
85 if (StringUtils.isNotBlank(filterKeys.getFilterByValue())) {
86 respKeys.setFilterByValue(filterKeys.getFilterByValue());
90 processKeys(respKeys, parameters.getName());
93 public void processKeys(ResponseKey filterKey, String aaiKey) {
95 String fn = "AaiInterfaceRulesHandler.processKeys()::";
96 log.info(fn + "processing for " + aaiKey);
97 String values = new String();
98 JSONObject aaiKeyValues = null;
99 log.info("Aai Data in Context : " + context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
100 if (context.getAttribute(AaiClientConstant.AAI_KEY_VALUES) != null) {
101 aaiKeyValues = new JSONObject(context.getAttribute(AaiClientConstant.AAI_KEY_VALUES));
102 log.info("Aai data already exsits : " + aaiKeyValues.toString());
104 aaiKeyValues = new JSONObject();
107 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnf")) {
108 values = getVnfDetailsFromContext(filterKey.getFieldKeyName());
111 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vnfc")) {
112 values = getVnfcDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
113 filterKey.getFilterByValue());
115 if (StringUtils.equalsIgnoreCase(filterKey.getUniqueKeyValue(), "vserver")) {
116 values = getVServerDetailsFromContext(filterKey.getFieldKeyName(), filterKey.getFilterByField(),
117 filterKey.getFilterByValue());
119 aaiKeyValues.put(aaiKey, values);
120 context.setAttribute(AaiClientConstant.AAI_KEY_VALUES, aaiKeyValues.toString());
124 private String getVServerDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
125 String fn = "AaiInterfaceRulesHander::getVServerDetailsFromContext():";
127 log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByName:" + filterByField + " FilterByValue:"
130 if (!StringUtils.equalsIgnoreCase(fieldKeyName, "vserver-name")) {
131 log.info(fn + "Returning values:" + values);
135 if (StringUtils.isNotEmpty(filterByField)
136 && StringUtils.isNotEmpty(filterByValue)) {
138 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
141 if (!StringUtils.equalsIgnoreCase(filterByField, "vm-number")) {
145 int vmNumber = Integer.parseInt(filterByValue);
146 if (vmNumber != vmIndex) {
150 if (StringUtils.isBlank(values)) {
151 values = vm.getVserverName();
153 values = values + "," + vm.getVserverName();
157 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
158 if (StringUtils.isBlank(values)) {
159 values = vm.getVserverName();
161 values = values + "," + vm.getVserverName();
166 log.info(fn + "Returning values:" + values);
170 //split from getVnfcDetailsFromContext
171 private String add2ValuesIpaddressV4OamVipNotEmpty(String values, String filterByField, String filterByValue) {
172 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
173 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
174 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
175 || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
179 if (StringUtils.isBlank(values)) {
180 values = vnfcInfo.getVnfcOamIpAddress();
182 values = values + "," + vnfcInfo.getVnfcOamIpAddress();
190 //split from getVnfcDetailsFromContext
191 private String add2ValuesIpaddressV4OamVipEmpty(String values, String filterByField, String filterByValue) {
192 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
193 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
194 if (StringUtils.isBlank(values)) {
195 values = vnfcInfo.getVnfcOamIpAddress();
197 values = values + "," + vnfcInfo.getVnfcOamIpAddress();
204 //split from getVnfcDetailsFromContext
205 private String add2ValuesVnfcNameNotEmpty(String values, String filterByField, String filterByValue) {
206 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
207 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
208 if (!StringUtils.equalsIgnoreCase(filterByField, "vnfc-function-code")
209 || !StringUtils.equalsIgnoreCase(filterByValue, vnfcInfo.getVnfcFunctionCode())) {
213 if (StringUtils.isBlank(values)) {
214 values = vnfcInfo.getVnfcName();
216 values = values + "," + vnfcInfo.getVnfcName();
224 //split from getVnfcDetailsFromContext
225 private String add2ValuesVnfcNameEmpty(String values, String filterByField, String filterByValue) {
226 for (AaiVmInfo vm : vnfInfoData.getVmInfo()) {
227 for (AaiVnfcInfo vnfcInfo : vm.getVnfcInfo()) {
228 if (StringUtils.isBlank(values)) {
229 values = vnfcInfo.getVnfcName();
231 values = values + "," + vnfcInfo.getVnfcName();
238 private String getVnfcDetailsFromContext(String fieldKeyName, String filterByField, String filterByValue) {
239 String fn = "AaiInterfaceRulesHander::getVnfcDetailsFromContext()";
241 log.info(fn + "FieldKeyName:" + fieldKeyName + " FilterByField:" + filterByField + " FilterByValue:"
243 if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipaddress-v4-oam-vip")) {
244 if (StringUtils.isNotEmpty(filterByField) && StringUtils.isNotEmpty(filterByValue)) {
245 values = add2ValuesIpaddressV4OamVipNotEmpty(values, filterByField, filterByValue);
247 values = add2ValuesIpaddressV4OamVipEmpty(values, filterByField, filterByValue);
250 if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnfc-name")) {
251 if (StringUtils.isNotBlank(filterByField) && StringUtils.isNotBlank(filterByValue)) {
252 values = add2ValuesVnfcNameNotEmpty(values, filterByField, filterByValue);
254 values = add2ValuesVnfcNameEmpty(values, filterByField, filterByValue);
257 log.info(fn + "Returning values:" + values);
261 private String getVnfDetailsFromContext(String fieldKeyName) {
263 log.info("getVnfDetailsFromContext::" + fieldKeyName);
265 if (StringUtils.equalsIgnoreCase(fieldKeyName, "vnf-name")) {
266 String vnfName = context.getAttribute("tmp.vnfInfo.vnf.vnf-name");
269 if (StringUtils.equalsIgnoreCase(fieldKeyName, "ipv4-oam-ipaddress")) {
270 String ipv4OamAddress = context.getAttribute("tmp.vnfInfo.vnf.ipv4-oam-address");
271 values = ipv4OamAddress;
276 public AaiVnfInfo getVnfInfoData() {
280 public void setVnfInfoData(AaiVnfInfo vnfInfoData) {
281 this.vnfInfoData = vnfInfoData;
284 public AaiVnfInfo generateAaiVnfInfoData() {
286 log.info("AaiInterfaceRulesHandlerImpl:generateAaiVnfInfoData(): Printing variables in context");
287 for (Object key : context.getAttributeKeySet()) {
288 String parmName = (String) key;
289 String parmValue = context.getAttribute(parmName);
290 log.debug("generateAaiVnfInfoData():: " + parmName + "=" + parmValue);
294 String vmcount = context.getAttribute("tmp.vnfInfo.vm-count");
296 if (!StringUtils.isBlank(vmcount)) {
297 vmCount = Integer.parseInt(vmcount);
299 log.info("generateAaiVnfInfoData::" + "vmCount:" + vmCount);
300 AaiVnfInfo vnfInfo = new AaiVnfInfo();
301 vnfInfo.setVnfName("vnf-name");
302 ArrayList<AaiVmInfo> vmList = new ArrayList<AaiVmInfo>();
304 for (int i = 0; i < vmCount; i++) {
305 AaiVmInfo vm = new AaiVmInfo();
306 String vnfcCountStr = context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-count");
307 int vnfcCount = Integer.parseInt(vnfcCountStr);
308 ArrayList<AaiVnfcInfo> vnfcInfoList = new ArrayList<AaiVnfcInfo>();
309 for (int j = 0; j < vnfcCount; j++) {
310 AaiVnfcInfo vnfcInfo = new AaiVnfcInfo();
311 vnfcInfo.setVnfcName(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-name"));
312 vnfcInfo.setVnfcFunctionCode(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-function-code"));
313 vnfcInfo.setVnfcOamIpAddress(
314 context.getAttribute("tmp.vnfInfo.vm[" + i + "].vnfc-ipaddress-v4-oam-vip"));
315 vnfcInfoList.add(vnfcInfo);
317 vm.setVnfcInfo(vnfcInfoList);
318 vm.setVserverId(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-id"));
319 vm.setVserverName(context.getAttribute("tmp.vnfInfo.vm[" + i + "].vserver-name"));
322 vnfInfo.setVmInfo(vmList);