aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dg/appc-dg-shared/appc-dg-aai/src/main/java/org/openecomp/appc/dg/aai/impl/AAIPluginImpl.java
blob: 830147ca2761ae03922567a520c528a6fd52474d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.dg.aai.impl;
import org.openecomp.appc.domainmodel.Vnf;
import org.openecomp.appc.domainmodel.Vnfc;
import org.openecomp.appc.domainmodel.Vserver;
import org.openecomp.appc.exceptions.APPCException;
import org.openecomp.appc.i18n.Msg;
import com.att.eelf.i18n.EELFResourceManager;

import org.openecomp.appc.dg.aai.AAIPlugin;
import org.openecomp.appc.dg.aai.exception.AAIQueryException;
import org.openecomp.appc.dg.aai.objects.AAIQueryResult;
import org.openecomp.appc.dg.aai.objects.Relationship;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.openecomp.sdnc.sli.SvcLogicContext;
import org.openecomp.sdnc.sli.SvcLogicException;
import org.openecomp.sdnc.sli.SvcLogicResource;
import org.openecomp.sdnc.sli.aai.AAIClient;
import org.openecomp.sdnc.sli.aai.AAIService;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;

import java.util.*;


public class AAIPluginImpl implements AAIPlugin {
    private AAIClient aaiClient;
    private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIPluginImpl.class);

    public AAIPluginImpl() {
        BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
        ServiceReference sref = bctx.getServiceReference(AAIService.class);
        aaiClient = (AAIClient) bctx.getService(sref);
    }

    @Override
    public void postGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        String vnf_id = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
        String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);

        String key = "vnf-id = '" + vnf_id + "'";

        Map<String, String> data = new HashMap<>();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            String paramKey = entry.getKey();
            int pos = paramKey.indexOf(Constants.AAI_INPUT_DATA);
            if (pos == 0) {
                data.put(paramKey.substring(Constants.AAI_INPUT_DATA.length()+1), entry.getValue());
            }
        }

        try {
            SvcLogicResource.QueryStatus response = aaiClient.update("generic-vnf", key, data, prefix, ctx);
            if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
                String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnf_id);
                ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
                throw new APPCException(msg);
            }
            logger.info("AAIResponse: " + response.toString());
            if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
                String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
                ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
                throw new APPCException(msg);
            }
            String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "PostGenericVnfData", "VNF ID " + vnf_id);
            ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);

        } catch (SvcLogicException e) {
            String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
            ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
            logger.error(msg);
            throw new APPCException(e);
        }
    }

    @Override
    public void getGenericVnfData(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        String vnf_id = ctx.getAttribute(Constants.VNF_ID_PARAM_NAME);
        String prefix = ctx.getAttribute(Constants.AAI_PREFIX_PARAM_NAME);

        String key = "vnf-id = '" + vnf_id + "'";
        try {
            SvcLogicResource.QueryStatus response = aaiClient.query("generic-vnf", false, null, key, prefix, null, ctx);
            if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
                String msg = EELFResourceManager.format(Msg.VNF_NOT_FOUND, vnf_id);
//                String errorMessage = String.format("VNF not found for vnf_id = %s", vnf_id);
                ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
                throw new APPCException(msg);
            } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
                String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
                ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
                throw new APPCException(msg);
            }
            String aaiEntitlementPoolUuid = ctx.getAttribute(Constants.AAI_ENTITLMENT_POOL_UUID_NAME);
            if (null == aaiEntitlementPoolUuid) aaiEntitlementPoolUuid = "";
            String aaiLicenseKeyGroupUuid = ctx.getAttribute(Constants.AAI_LICENSE_KEY_UUID_NAME);
            if (null == aaiLicenseKeyGroupUuid) aaiLicenseKeyGroupUuid = "";

            ctx.setAttribute(Constants.IS_RELEASE_ENTITLEMENT_REQUIRE, Boolean.toString(!aaiEntitlementPoolUuid.isEmpty()));
            ctx.setAttribute(Constants.IS_RELEASE_LICENSE_REQUIRE, Boolean.toString(!aaiLicenseKeyGroupUuid.isEmpty()));
            String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetGenericVnfData","VNF ID " + vnf_id);
            ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);

            logger.info("AAIResponse: " + response.toString());
        } catch (SvcLogicException e) {
            String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnf_id);
            ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
            logger.error(msg);
            throw new APPCException(e);
        }
    }

    @Override
    public void getVnfHierarchy(Map<String, String> params, SvcLogicContext ctx) throws APPCException {

        Map<Vnfc,Set<Vserver>> vnfcMap = new HashMap<>();
        String vnfType,vnfVersion = null;
        String vnfId = params.get("resourceKey");
        AAIQueryResult vnfQueryResult = null;
        int vmCount =0;
        try {
            vnfQueryResult = readVnf(vnfId);

            vnfType = vnfQueryResult.getAdditionProperties().get("vnf-type");
            vnfVersion = vnfQueryResult.getAdditionProperties().get("persona-model-version");

            for(Relationship vnfRelationship:vnfQueryResult.getRelationshipList()){
                if("vserver".equalsIgnoreCase(vnfRelationship.getRelatedTo())){
                    vmCount++;
                    String tenantId = vnfRelationship.getRelationShipDataMap().get("tenant.tenant-id");
                    String vmId = vnfRelationship.getRelationShipDataMap().get("vserver.vserver-id");
                    String vmRelatedLink = vnfRelationship.getRelatedLink();
                    String vmName = vnfRelationship.getRelatedProperties().get("vserver.vserver-name");
                    String cloudOwner = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-owner");
                    String cloudRegionId = vnfRelationship.getRelationShipDataMap().get("cloud-region.cloud-region-id");

                    AAIQueryResult vmQueryResult = readVM(vmId,tenantId,cloudOwner,cloudRegionId);
                    String vmURL = vmQueryResult.getAdditionProperties().get("vserver-selflink");

                    Vserver vm = new Vserver(vmURL,tenantId,vmId,vmRelatedLink,vmName);
                    for(Relationship vmRelation:vmQueryResult.getRelationshipList()){

                        if("vnfc".equalsIgnoreCase(vmRelation.getRelatedTo())){
                            String vnfcName = vmRelation.getRelationShipDataMap().get("vnfc.vnfc-name");
                            AAIQueryResult vnfcQueryResult = readVnfc(vnfcName);
                            String vnfcType = vnfcQueryResult.getAdditionProperties().get("vnfc-type");

                            Vnfc vnfc = new Vnfc(vnfcType,null,vnfcName);
                            Set<Vserver> vmSet = vnfcMap.get(vnfc);
                            if(vmSet == null){
                                vmSet = new HashSet<>();
                                vnfcMap.put(vnfc,vmSet);
                            }
                            vmSet.add(vm);
                        }
                    }
                }
            }
            ctx.setAttribute("VNF.VMCount",String.valueOf(vmCount));
        } catch (AAIQueryException e) {
            ctx.setAttribute("getVnfHierarchy_result", "FAILURE");
            String msg = EELFResourceManager.format(Msg.AAI_QUERY_FAILED, vnfId);
            ctx.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
            logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: " + ctx
                    .getAttribute("getResource_result"));
            logger.warn("Incorrect or Incomplete VNF Hierarchy");
            throw new APPCException("Error Retrieving VNF hierarchy");
        }

        Vnf vnf = new Vnf(vnfId,vnfType,vnfVersion);
        for(Vnfc vnfc:vnfcMap.keySet()){
            for(Vserver vm:vnfcMap.get(vnfc)){
                vnfc.addVm(vm);
            }
            vnf.addVnfc(vnfc);
        }

        populateContext(vnf,ctx);
        ctx.setAttribute("getVnfHierarchy_result", "SUCCESS");
        String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy","VNF ID " + vnfId);
        ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);

    }

    private void populateContext(Vnf vnf ,SvcLogicContext ctx) {
        ctx.setAttribute("vnf.type",vnf.getVnfType());
        ctx.setAttribute("vnf.version",vnf.getVnfVersion());
        ctx.setAttribute("vnf.vnfcCount",String.valueOf(vnf.getVnfcs().size()));
        int vnfcCount =0;
        for(Vnfc vnfc:vnf.getVnfcs()){
            ctx.setAttribute("vnf.vnfc["+vnfcCount+"].name",vnfc.getVnfcName());
            ctx.setAttribute("vnf.vnfc["+vnfcCount+"].type",vnfc.getVnfcType());
            ctx.setAttribute("vnf.vnfc["+vnfcCount+"].vm_count",String.valueOf(vnfc.getVserverList().size()));
            int vmCount =0;
            for(Vserver vm:vnfc.getVserverList()){
                ctx.setAttribute("vnf.vnfc["+vnfcCount+"].vm["+ vmCount++ +"].url",vm.getUrl());
            }
            vnfcCount++;
        }
    }

    private AAIQueryResult readVnfc(String vnfcName) throws AAIQueryException {
        String query = "vnfc.vnfc-name = '" + vnfcName + "'";
        String prefix = "VNFC";
        String resourceType = "vnfc";
        SvcLogicContext vnfContext = readResource(query,prefix,resourceType);
        String[] additionalProperties = new String[]{"vnfc-type","vnfc-name",
                "vnfc-function-code","in-maint","prov-status",
                "is-closed-loop-disabled","orchestration-status","resource-version"};
        AAIQueryResult result = readRelationDataAndProperties(prefix, vnfContext,additionalProperties);
        return result;
    }

    private AAIQueryResult readVM(String vmId,String tenantId,String cloudOwner,String cloudRegionId) throws AAIQueryException {
        String query = "vserver.vserver-id = '" + vmId + "' AND tenant.tenant_id = '" + tenantId + "' AND cloud-region.cloud-owner = '"
                + cloudOwner + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
        String prefix = "VM";
        String resourceType = "vserver";
        SvcLogicContext vnfContext = readResource(query,prefix,resourceType);
        String[] additionalProperties = new String[]{"vserver-id","vserver-selflink",
                                                "vserver-name","in-maint","prov-status","is-closed-loop-disabled",
                                                "vserver-name2","resource-version",};
        AAIQueryResult result = readRelationDataAndProperties(prefix, vnfContext,additionalProperties);

        return result;
    }

    private AAIQueryResult readVnf(String vnfId) throws AAIQueryException {
        String query = "generic-vnf.vnf-id = '" + vnfId + "'";
        String prefix = "VNF";
        String resourceType = "generic-vnf";
        SvcLogicContext vnfContext = readResource(query,prefix,resourceType);

        String[] additionalProperties = new String[]{"vnf-type","vnf-name",
                "in-maint","prov-status","heat-stack-id",
                "is-closed-loop-disabled","orchestration-status","resource-version","persona-model-version"};

        AAIQueryResult result = readRelationDataAndProperties(prefix, vnfContext,additionalProperties);

        return result;
    }

    private AAIQueryResult readRelationDataAndProperties(String prefix, SvcLogicContext context,String[] additionalProperties) {
        AAIQueryResult result = new AAIQueryResult();

        Integer relationsCount = Integer.parseInt(context.getAttribute(prefix + ".relationship-list.relationship_length"));
        for(int i=0;i<relationsCount;i++){
            Relationship relationShip = new Relationship();
            relationShip.setRelatedLink(context.getAttribute(prefix + ".relationship-list.relationship["+i+"].related-link"));
            relationShip.setRelatedTo(context.getAttribute(prefix + ".relationship-list.relationship["+i+"].related-to"));
            Integer relationDataCount = Integer.parseInt(context.getAttribute(prefix + ".relationship-list.relationship["+i+"].relationship-data_length"));
            for(int j=0;j<relationDataCount;j++){
                String key = context.getAttribute(prefix+".relationship-list.relationship["+i+"].relationship-data["+j+"].relationship-key");
                String value = context.getAttribute(prefix+".relationship-list.relationship["+i+"].relationship-data["+j+"].relationship-value");
                relationShip.getRelationShipDataMap().put(key,value);
            }
            Integer relatedPropertyCount = 0;
            String relatedPropertyCountStr = null;
            try{
                relatedPropertyCountStr =context.getAttribute(prefix + ".relationship-list.relationship["+i+"].related-to-property_length");
                relatedPropertyCount = Integer.parseInt(relatedPropertyCountStr);
            }
            catch (NumberFormatException e){
                logger.debug("Invalid value in the context for Related Property Count " + relatedPropertyCountStr);
            }

            for(int j=0;j<relatedPropertyCount;j++){
                String key = context.getAttribute(prefix+".relationship-list.relationship["+i+"].related-to-property["+j+"].property-key");
                String value = context.getAttribute(prefix+".relationship-list.relationship["+i+"].related-to-property["+j+"].property-value");
                relationShip.getRelatedProperties().put(key,value);
            }
            result.getRelationshipList().add(relationShip);
        }

        for(String key:additionalProperties){
            result.getAdditionProperties().put(key,context.getAttribute(prefix+"."+key));
        }
        return result;
    }

    private SvcLogicContext readResource(String query, String prefix, String resourceType) throws AAIQueryException {
        SvcLogicContext resourceContext = new SvcLogicContext();
        try {
            SvcLogicResource.QueryStatus response = aaiClient.query(resourceType,false,null,query,prefix,null,resourceContext);
            logger.info("AAIResponse: " + response.toString());
            if(!SvcLogicResource.QueryStatus.SUCCESS.equals(response)){
                throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
            }
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, query, "", e.getMessage()));
            throw new AAIQueryException("Error Retrieving VNF hierarchy from A&AI");
        }
        return resourceContext;
    }

    @Override public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
                params.get("resourceKey");
        if (logger.isDebugEnabled()) {
            logger.debug("inside getResorce");
            logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
        }
        try {
            SvcLogicResource.QueryStatus response =
                    aaiClient.query(resourceType, false, null, resourceKey, ctx_prefix, null, ctx);
            logger.info("AAIResponse: " + response.toString());
            ctx.setAttribute("getResource_result", response.toString());
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, "", e.getMessage()));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("exiting getResource======");
        }
    }

    @Override public void postResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        String resourceType = params.get("resourceType"), ctx_prefix = params.get("prefix"), resourceKey =
                params.get("resourceKey"), att_name = params.get("attributeName"), att_value =
                params.get("attributeValue");
        if (logger.isDebugEnabled()) {
            logger.debug("inside postResource");
            logger.debug("Updating " + resourceType + " details in A&AI for Key : " + resourceKey);
            logger.debug("Updating " + att_name + " to : " + att_value);
        }
        Map<String, String> data = new HashMap<String, String>();
        data.put(att_name, att_value);

        try {
            SvcLogicResource.QueryStatus response = aaiClient.update(resourceType, resourceKey, data, ctx_prefix, ctx);
            logger.info("AAIResponse: " + response.toString());
            ctx.setAttribute("postResource_result", response.toString());
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, att_value, e.getMessage()));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("exiting postResource======");
        }
    }

    @Override public void deleteResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        String resourceType = params.get("resourceType"), resourceKey = params.get("resourceKey");

        if (logger.isDebugEnabled()) {
            logger.debug("inside deleteResource");
            logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
        }
        try {
            SvcLogicResource.QueryStatus response = aaiClient.delete(resourceType, resourceKey, ctx);
            logger.info("AAIResponse: " + response.toString());
            ctx.setAttribute("deleteResource_result", response.toString());
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey, e.getMessage()));
        }
        if (logger.isDebugEnabled()) {
            logger.debug("exiting deleteResource======");
        }
    }
}