Applying license changes to all files
[appc.git] / appc-dg / appc-dg-shared / appc-dg-aai / src / main / java / org / openecomp / appc / dg / aai / impl / AAIPluginImpl.java
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.openecomp.appc.dg.aai.impl;
26 import org.openecomp.appc.domainmodel.Vnf;
27 import org.openecomp.appc.domainmodel.Vnfc;
28 import org.openecomp.appc.domainmodel.Vserver;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.appc.i18n.Msg;
31 import com.att.eelf.i18n.EELFResourceManager;
32
33 import org.openecomp.appc.dg.aai.AAIPlugin;
34 import org.openecomp.appc.dg.aai.exception.AAIQueryException;
35 import org.openecomp.appc.dg.aai.objects.AAIQueryResult;
36 import org.openecomp.appc.dg.aai.objects.Relationship;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import org.openecomp.sdnc.sli.SvcLogicContext;
40 import org.openecomp.sdnc.sli.SvcLogicException;
41 import org.openecomp.sdnc.sli.SvcLogicResource;
42 import org.openecomp.sdnc.sli.aai.AAIClient;
43 import org.openecomp.sdnc.sli.aai.AAIService;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.FrameworkUtil;
46 import org.osgi.framework.ServiceReference;
47
48 import java.util.*;
49
50
51 public class AAIPluginImpl implements AAIPlugin {
52     private AAIClient aaiClient;
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIPluginImpl.class);
54
55     public AAIPluginImpl() {
56         BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
57         ServiceReference sref = bctx.getServiceReference(AAIService.class);
58         aaiClient = (AAIClient) bctx.getService(sref);
59     }
60
61     @Override
62     public void postGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
63         String vnf_id = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
64         String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
65
66         String key = "vnf-id = '" + vnf_id + "'";
67
68         Map<String, String> data = new HashMap<>();
69         for (Map.Entry<String, String> entry : params.entrySet()) {
70             String paramKey = entry.getKey();
71             int pos = paramKey.indexOf(Constants.AAI_INPUT_DATA);
72             if (pos == 0) {
73                 data.put(paramKey.substring(Constants.AAI_INPUT_DATA.length()+1), entry.getValue());
74             }
75         }
76
77         try {
78             SvcLogicResource.QueryStatus response = aaiClient.update("generic-vnf", key, data, prefix, ctx);
79             if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
80                 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnf_id);
81                 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
82                 throw new APPCException(msg);
83             }
84             logger.info("AAIResponse: " + response.toString());
85             if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
86                 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
87                 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
88                 throw new APPCException(msg);
89             }
90             String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "PostGenericVnfData", "VNF ID " + vnf_id);
91             ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
92
93         } catch (SvcLogicException e) {
94             String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
95             ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
96             logger.error(msg);
97             throw new APPCException(e);
98         }
99     }
100
101     @Override
102     public void getGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
103         String vnf_id = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
104         String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);
105
106         String key = "vnf-id = '" + vnf_id + "'";
107         try {
108             SvcLogicResource.QueryStatus response = aaiClient.query("generic-vnf", false, null, key, prefix, null, ctx);
109             if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
110                 String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnf_id);
111 //                String errorMessage = String.format("VNF not found for vnf_id = %s", vnf_id);
112                 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
113                 throw new APPCException(msg);
114             } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
115                 String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
116                 ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
117                 throw new APPCException(msg);
118             }
119             String aaiEntitlementPoolUuid = ctx.getAttribute(Constants.AAI_ENTITLMENT_POOL_UUID_NAME);
120             if (null == aaiEntitlementPoolUuid) aaiEntitlementPoolUuid = "";
121             String aaiLicenseKeyGroupUuid = ctx.getAttribute(Constants.AAI_LICENSE_KEY_UUID_NAME);
122             if (null == aaiLicenseKeyGroupUuid) aaiLicenseKeyGroupUuid = "";
123
124             ctx.setAttribute(Constants.IS_RELEASE_ENTITLEMENT_REQUIRE, Boolean.toString(!aaiEntitlementPoolUuid.isEmpty()));
125             ctx.setAttribute(Constants.IS_RELEASE_LICENSE_REQUIRE, Boolean.toString(!aaiLicenseKeyGroupUuid.isEmpty()));
126             String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetGenericVnfData","VNF ID " + vnf_id);
127             ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
128
129             logger.info("AAIResponse: " + response.toString());
130         } catch (SvcLogicException e) {
131             String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
132             ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
133             logger.error(msg);
134             throw new APPCException(e);
135         }
136     }
137
138     @Override
139     public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
140
141         Map<Vnfc,Set<Vserver>> vnfcMap = new HashMap<>();
142         String vnfType,vnfVersion = null;
143         String vnfId = params.get("resourceKey");
144         AAIQueryResult vnfQueryResult = null;
145         int vmCount =0;
146         try {
147             vnfQueryResult = readVnf(vnfId);
148
149             vnfType = vnfQueryResult.getAdditionProperties().get("vnf-type");
150             vnfVersion = vnfQueryResult.getAdditionProperties().get("persona-model-version");
151
152             for(Relationship vnfRelationship:vnfQueryResult.getRelationshipList()){
153                 if("vserver".equalsIgnoreCase(vnfRelationship.getRelatedTo())){
154                     vmCount++;
155                     String tenantId = vnfRelationship.getRelationShipDataMap().get("tenant.tenant-id");
156                     String vmId = vnfRelationship.getRelationShipDataMap().get("vserver.vserver-id");
157                     String vmRelatedLink = vnfRelationship.getRelatedLink();
158                     String vmName = vnfRelationship.getRelatedProperties().get("vserver.vserver-name");
159                     String cloudOwner = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-owner");
160                     String cloudRegionId = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-region-id");
161
162                     AAIQueryResult vmQueryResult = readVM(vmId,tenantId,cloudOwner,cloudRegionId);
163                     String vmURL = vmQueryResult.getAdditionProperties().get("vserver-selflink");
164
165                     Vserver vm = new Vserver(vmURL,tenantId,vmId,vmRelatedLink,vmName);
166                     for(Relationship vmRelation:vmQueryResult.getRelationshipList()){
167
168                         if("vnfc".equalsIgnoreCase(vmRelation.getRelatedTo())){
169                             String vnfcName = vmRelation.getRelationShipDataMap().get("vnfc.vnfc-name");
170                             AAIQueryResult vnfcQueryResult = readVnfc(vnfcName);
171                             String vnfcType = vnfcQueryResult.getAdditionProperties().get("vnfc-type");
172
173                             Vnfc vnfc = new Vnfc(vnfcType,null,vnfcName);
174                             Set<Vserver> vmSet = vnfcMap.get(vnfc);
175                             if(vmSet == null){
176                                 vmSet = new HashSet<>();
177                                 vnfcMap.put(vnfc,vmSet);
178                             }
179                             vmSet.add(vm);
180                         }
181                     }
182                 }
183             }
184             ctx.setAttribute("VNF.VMCount",String.valueOf(vmCount));
185         } catch (AAIQueryException e) {
186             ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
187             String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
188             ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
189             logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
190                     .getAttribute("getResource_result"));
191             logger.warn("Incorrect or Incomplete VNF Hierarchy");
192             throw new APPCException("Error Retrieving VNF hierarchy");
193         }
194
195         Vnf vnf = new Vnf(vnfId,vnfType,vnfVersion);
196         for(Vnfc vnfc:vnfcMap.keySet()){
197             for(Vserver vm:vnfcMap.get(vnfc)){
198                 vnfc.addVm(vm);
199             }
200             vnf.addVnfc(vnfc);
201         }
202
203         populateContext(vnf,ctx);
204         ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
205         String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy","VNF ID " + vnfId);
206         ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
207
208     }
209
210     private void populateContext(Vnf vnf ,SvcLogicContext ctx) {
211         ctx.setAttribute("vnf.type",vnf.getVnfType());
212         ctx.setAttribute("vnf.version",vnf.getVnfVersion());
213         ctx.setAttribute("vnf.vnfcCount",String.valueOf(vnf.getVnfcs().size()));
214         int vnfcCount =0;
215         for(Vnfc vnfc:vnf.getVnfcs()){
216             ctx.setAttribute("vnf.vnfc["+vnfcCount+"].name",vnfc.getVnfcName());
217             ctx.setAttribute("vnf.vnfc["+vnfcCount+"].type",vnfc.getVnfcType());
218             ctx.setAttribute("vnf.vnfc["+vnfcCount+"].vm_count",String.valueOf(vnfc.getVserverList().size()));
219             int vmCount =0;
220             for(Vserver vm:vnfc.getVserverList()){
221                 ctx.setAttribute("vnf.vnfc["+vnfcCount+"].vm["+ vmCount++ +"].url",vm.getUrl());
222             }
223             vnfcCount++;
224         }
225     }
226
227     private AAIQueryResult readVnfc(String vnfcName) throws AAIQueryException {
228         String query = "vnfc.vnfc-name = '" + vnfcName + "'";
229         String prefix = "VNFC";
230         String resourceType = "vnfc";
231         SvcLogicContext vnfContext = readResource(query,prefix,resourceType);
232         String[] additionalProperties = new String[]{"vnfc-type","vnfc-name",
233                 "vnfc-function-code","in-maint","prov-status",
234                 "is-closed-loop-disabled","orchestration-status","resource-version"};
235         AAIQueryResult result = readRelationDataAndProperties(prefix, vnfContext,additionalProperties);
236         return result;
237     }
238
239     private AAIQueryResult readVM(String vmId,String tenantId,String cloudOwner,String cloudRegionId) throws AAIQueryException {
240         String query = "vserver.vserver-id = '" + vmId + "' AND tenant.tenant_id = '" + tenantId + "' AND cloud-region.cloud-owner = '"
241                 + cloudOwner + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
242         String prefix = "VM";
243         String resourceType = "vserver";
244         SvcLogicContext vnfContext = readResource(query,prefix,resourceType);
245         String[] additionalProperties = new String[]{"vserver-id","vserver-selflink",
246                                                 "vserver-name","in-maint","prov-status","is-closed-loop-disabled",
247                                                 "vserver-name2","resource-version",};
248         AAIQueryResult result = readRelationDataAndProperties(prefix, vnfContext,additionalProperties);
249
250         return result;
251     }
252
253     private AAIQueryResult readVnf(String vnfId) throws AAIQueryException {
254         String query = "generic-vnf.vnf-id = '" + vnfId + "'";
255         String prefix = "VNF";
256         String resourceType = "generic-vnf";
257         SvcLogicContext vnfContext = readResource(query,prefix,resourceType);
258
259         String[] additionalProperties = new String[]{"vnf-type","vnf-name",
260                 "in-maint","prov-status","heat-stack-id",
261                 "is-closed-loop-disabled","orchestration-status","resource-version","persona-model-version"};
262
263         AAIQueryResult result = readRelationDataAndProperties(prefix, vnfContext,additionalProperties);
264
265         return result;
266     }
267
268     private AAIQueryResult readRelationDataAndProperties(String prefix, SvcLogicContext context,String[] additionalProperties) {
269         AAIQueryResult result = new AAIQueryResult();
270
271         Integer relationsCount = Integer.parseInt(context.getAttribute(prefix + ".relationship-list.relationship_length"));
272         for(int i=0;i<relationsCount;i++){
273             Relationship relationShip = new Relationship();
274             relationShip.setRelatedLink(context.getAttribute(prefix + ".relationship-list.relationship["+i+"].related-link"));
275             relationShip.setRelatedTo(context.getAttribute(prefix + ".relationship-list.relationship["+i+"].related-to"));
276             Integer relationDataCount = Integer.parseInt(context.getAttribute(prefix + ".relationship-list.relationship["+i+"].relationship-data_length"));
277             for(int j=0;j<relationDataCount;j++){
278                 String key = context.getAttribute(prefix+".relationship-list.relationship["+i+"].relationship-data["+j+"].relationship-key");
279                 String value = context.getAttribute(prefix+".relationship-list.relationship["+i+"].relationship-data["+j+"].relationship-value");
280                 relationShip.getRelationShipDataMap().put(key,value);
281             }
282             Integer relatedPropertyCount = 0;
283             String relatedPropertyCountStr = null;
284             try{
285                 relatedPropertyCountStr =context.getAttribute(prefix + ".relationship-list.relationship["+i+"].related-to-property_length");
286                 relatedPropertyCount = Integer.parseInt(relatedPropertyCountStr);
287             }
288             catch (NumberFormatException e){
289                 logger.debug("Invalid value in the context for Related Property Count " + relatedPropertyCountStr);
290             }
291
292             for(int j=0;j<relatedPropertyCount;j++){
293                 String key = context.getAttribute(prefix+".relationship-list.relationship["+i+"].related-to-property["+j+"].property-key");
294                 String value = context.getAttribute(prefix+".relationship-list.relationship["+i+"].related-to-property["+j+"].property-value");
295                 relationShip.getRelatedProperties().put(key,value);
296             }
297             result.getRelationshipList().add(relationShip);
298         }
299
300         for(String key:additionalProperties){
301             result.getAdditionProperties().put(key,context.getAttribute(prefix+"."+key));
302         }
303         return result;
304     }
305
306     private SvcLogicContext readResource(String query, String prefix, String resourceType) throws AAIQueryException {
307         SvcLogicContext resourceContext = new SvcLogicContext();
308         try {
309             SvcLogicResource.QueryStatus response = aaiClient.query(resourceType,false,null,query,prefix,null,resourceContext);
310             logger.info("AAIResponse: " + response.toString());
311             if(!SvcLogicResource.QueryStatus.SUCCESS.equals(response)){
312                 throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
313             }
314         } catch (SvcLogicException e) {
315             logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, query, "", e.getMessage()));
316             throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
317         }
318         return resourceContext;
319     }
320
321     @Override public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
322         String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
323                 params.get("resourceKey");
324         if (logger.isDebugEnabled()) {
325             logger.debug("inside getResorce");
326             logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
327         }
328         try {
329             SvcLogicResource.QueryStatus response =
330                     aaiClient.query(resourceType, false, null, resourceKey, ctx_prefix, null, ctx);
331             logger.info("AAIResponse: " + response.toString());
332             ctx.setAttribute("getResource_result", response.toString());
333         } catch (SvcLogicException e) {
334             logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, "", e.getMessage()));
335         }
336         if (logger.isDebugEnabled()) {
337             logger.debug("exiting getResource======");
338         }
339     }
340
341     @Override public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
342         String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
343                 params.get("resourceKey"), att_name = params.get("attributeName"), att_value =
344                 params.get("attributeValue");
345         if (logger.isDebugEnabled()) {
346             logger.debug("inside postResource");
347             logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
348             logger.debug("Updating " + att_name + " to : " + att_value);
349         }
350         Map<String, String> data = new HashMap<String, String>();
351         data.put(att_name, att_value);
352
353         try {
354             SvcLogicResource.QueryStatus response = aaiClient.update(resourceType, resourceKey, data, ctx_prefix, ctx);
355             logger.info("AAIResponse: " + response.toString());
356             ctx.setAttribute("postResource_result", response.toString());
357         } catch (SvcLogicException e) {
358             logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, att_value, e.getMessage()));
359         }
360         if (logger.isDebugEnabled()) {
361             logger.debug("exiting postResource======");
362         }
363     }
364
365     @Override public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
366         String resourceType = params.get("resourceType"), resourceKey = params.get("resourceKey");
367
368         if (logger.isDebugEnabled()) {
369             logger.debug("inside deleteResource");
370             logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
371         }
372         try {
373             SvcLogicResource.QueryStatus response = aaiClient.delete(resourceType, resourceKey, ctx);
374             logger.info("AAIResponse: " + response.toString());
375             ctx.setAttribute("deleteResource_result", response.toString());
376         } catch (SvcLogicException e) {
377             logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey, e.getMessage()));
378         }
379         if (logger.isDebugEnabled()) {
380             logger.debug("exiting deleteResource======");
381         }
382     }
383 }