Changed to unmaintained
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / node / InventoryInfoExtractor.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
7  * =============================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.flow.controller.node;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.appc.flow.controller.interfaceData.InventoryInfo;
27 import org.onap.appc.flow.controller.interfaceData.Vm;
28 import org.onap.appc.flow.controller.interfaceData.VnfInfo;
29 import org.onap.appc.flow.controller.interfaceData.Vnfcslist;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31
32 /**
33  * Helper class for FlowControlNode
34  */
35 class InventoryInfoExtractor {
36
37     private static final EELFLogger log = EELFManager.getInstance().getLogger(InventoryInfoExtractor.class);
38
39     InventoryInfo getInventoryInfo(SvcLogicContext ctx, String vnfId) {
40         String fn = "InventoryInfoExtractor.getInventoryInfo";
41
42         VnfInfo vnfInfo = new VnfInfo();
43         vnfInfo.setVnfId(vnfId);
44         vnfInfo.setVnfName(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-name"));
45         vnfInfo.setVnfType(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"));
46         vnfInfo.setIdentityUrl(getIdentityUrl(ctx, vnfInfo, vnfId));
47
48         String vmcount = ctx.getAttribute("tmp.vnfInfo.vm-count");
49         log.info(fn + "vmcount:" + vmcount);
50
51         int vmCount = (StringUtils.isNotBlank(vmcount)) ? Integer.parseInt(vmcount) : 0;
52
53         for (int i = 0; i < vmCount; i++) {
54             processVm(ctx, vnfInfo, i);
55         }
56
57         InventoryInfo inventoryInfo = new InventoryInfo();
58         inventoryInfo.setVnfInfo(vnfInfo);
59         log.info(fn + "Inventory Output:" + inventoryInfo.toString());
60
61         return inventoryInfo;
62     }
63
64     private void processVm(SvcLogicContext ctx, VnfInfo vnfInfo, int index) {
65         Vm vm = new Vm();
66         vm.setVserverId(ctx.getAttribute("tmp.vnfInfo.vm[" + index + "].vserver-id"));
67         vm.setVmId(ctx.getAttribute("tmp.vnfInfo.vm[" + index + "].vserver-selflink"));
68         int vnfcCount = Integer.parseInt(ctx.getAttribute("tmp.vnfInfo.vm[" + index + "].vnfc-count"));
69         if (vnfcCount > 0) {
70             Vnfcslist vnfc = new Vnfcslist();
71             vnfc.setVnfcName(ctx.getAttribute("tmp.vnfInfo.vm[" + index + "].vnfc-name"));
72             vnfc.setVnfcType(ctx.getAttribute("tmp.vnfInfo.vm[" + index + "].vnfc-type"));
73             vnfc.setVnfcFunctionCode(ctx.getAttribute("tmp.vnfInfo.vm[" + index + "].vnfc-function-code"));
74             vm.setVnfc(vnfc);
75         }
76         vnfInfo.getVm().add(vm);
77     }
78
79     public String getIdentityUrl(SvcLogicContext ctx, VnfInfo vnfInfo, String vnfId) {
80         String identityUrl = "";
81         for (String key : ctx.getAttributeKeySet()) {
82             log.debug("InventoryData " + key + "=" + ctx.getAttribute(key));
83         }
84         String urlFromPayload = ctx.getAttribute("identity-url");
85         log.info("Url from payload:" + urlFromPayload);
86         String urlFromAAI = ctx.getAttribute("tmp.vnfInfo.cloud-region.identity-url");
87         log.info("Url from AAI:" + urlFromAAI);
88
89         if (StringUtils.isNotBlank(urlFromPayload)) {
90             identityUrl = urlFromPayload;
91         } else if (StringUtils.isNotBlank(urlFromAAI)) {
92             identityUrl = urlFromAAI;
93         }
94         return identityUrl;
95
96     }
97
98 }