aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dg-util/appc-dg-util-bundle/src/main/java/org/onap/appc/dg/util/impl/ExecuteNodeActionImpl.java
blob: abe24554dd28f51fe0e02ce511a380a8e21a9ffe (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=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * Modifications Copyright (C) 2018 Nokia
 * ================================================================================
 * Modifications Copyright (C) 2018 IBM.
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * ================================================================================
 * 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.onap.appc.dg.util.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.onap.appc.dg.util.ExecuteNodeAction;
import org.onap.appc.exceptions.APPCException;
import org.onap.appc.i18n.Msg;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.eelf.i18n.EELFResourceManager;

public class ExecuteNodeActionImpl implements ExecuteNodeAction {

    private static final String RESOURCE_TYPE_PARAM = "resourceType";
    private static final String RESOURCE_KEY_PARAM = "resourceKey";
    private static final String PREFIX_PARAM = "prefix";
    private static final String AAI_RESPONSE_STR = "AAIResponse: ";
    private static final String GET_RESOURCE_RESULT = "getResource_result";
    private static final String SUCCESS_PARAM = "SUCCESS";
    private static final String RELATIONSHIP_DATA_LEN_PARAM = "relationship-data_length";
    private static final String RELATIONSHIP_DATA_STR = "relationship-data[";
    private static final String VNFF_VM_STR = "VNF.VM[";
    private static final String VNF_VNFC_STR = "VNF.VNFC[";
    private static final String GET_VNF_HIERARCHY_RESULT_PARAM = "getVnfHierarchy_result";
    private static final String ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM = "Error Retrieving VNFC hierarchy";
    private static final String RELATED_TO_PROPERTY_LEN_PARAM = "related-to-property_length";
    public static final String DG_OUTPUT_STATUS_MESSAGE = "output.status.message";
    private static EELFLogger logger = EELFManager.getInstance().getLogger(ExecuteNodeActionImpl.class);


    private AAIServiceFactory aaiServiceFactory;

    public ExecuteNodeActionImpl(AAIServiceFactory aaiServiceFactory) {
        this.aaiServiceFactory = aaiServiceFactory;
    }

    /**
     * Method called in TestDG to test timeout scenario
     *
     * @param params
     *            waitTime time in millisecond DG is going to sleep
     */
    @Override
    public void waitMethod(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        try {
            String waitTime = params.get("waitTime");

            logger.info("DG will waits for " + Long.parseLong(waitTime) + "milliseconds");
            Thread.sleep(Long.parseLong(waitTime));
            logger.info("DG waits for " + Long.parseLong(waitTime) + " milliseconds completed");
        } catch (InterruptedException e) {
            logger.error("Error In ExecuteNodeActionImpl for waitMethod() due to InterruptedException: reason = "
                    + e.getMessage(), e);
            Thread.currentThread().interrupt();
        }
    }

    @Override
    public void getResource(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
        String resourceType = params.get(RESOURCE_TYPE_PARAM);
        String ctxPrefix = params.get(PREFIX_PARAM);
        String resourceKey = params.get(RESOURCE_KEY_PARAM);

        if (logger.isDebugEnabled()) {
            logger.debug("inside getResource");
            logger.debug("Retrieving " + resourceType + " details from A&AI for Key : " + resourceKey);
        }

        try {
            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().query(resourceType, false, null,
                    resourceKey, ctxPrefix, null, ctx);
            logger.info(AAI_RESPONSE_STR + response.toString());
            ctx.setAttribute(GET_RESOURCE_RESULT, response.toString());
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_GET_DATA_FAILED, resourceKey, ""), e);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("exiting getResource======");
        }
    }

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

        try {
            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().update(resourceType, resourceKey,
                    data, ctxPrefix, ctx);
            logger.info(AAI_RESPONSE_STR + response.toString());
            ctx.setAttribute("postResource_result", response.toString());
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_UPDATE_FAILED, resourceKey, attValue), e);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("exiting postResource======");
        }
    }

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

        if (logger.isDebugEnabled()) {
            logger.debug("inside deleteResource");
            logger.debug("Deleting " + resourceType + " details From A&AI for Key : " + resourceKey);
        }

        try {
            SvcLogicResource.QueryStatus response = aaiServiceFactory.getAAIService().delete(resourceType, resourceKey,
                    ctx);
            logger.info(AAI_RESPONSE_STR + response.toString());
            ctx.setAttribute("deleteResource_result", response.toString());
        } catch (SvcLogicException e) {
            logger.error(EELFResourceManager.format(Msg.AAI_DELETE_FAILED, resourceKey), e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("exiting deleteResource======");
        }
    }

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

      if (logger.isDebugEnabled()) {
            logger.debug("Inside getVnfHierarchy======");
        }
        String resourceKey = params.get(RESOURCE_KEY_PARAM);
        String retrivalVnfKey = "generic-vnf.vnf-id = '" + resourceKey + "'";
        Map<String, String> paramsVnf = new HashMap<>();
        paramsVnf.put(RESOURCE_TYPE_PARAM, "generic-vnf");
        paramsVnf.put(PREFIX_PARAM, "vnfRetrived");
        paramsVnf.put(RESOURCE_KEY_PARAM, retrivalVnfKey);
        logger.debug("Retrieving VNF details from A&AI");
        // Retrive all the relations of VNF
        SvcLogicContext vnfCtx = getSvcLogicContext();
        getResource(paramsVnf, vnfCtx);
        if (vnfCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
            trySetHeatStackIDAttribute(ctx, vnfCtx);
            ctx.setAttribute("vnf.type", vnfCtx.getAttribute("vnfRetrived.vnf-type"));
            Map<String, String> vnfHierarchyMap = new ConcurrentHashMap<>();
            Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
            int vmCount = 0;
            Set<String> vmSet;
            String vmURL;

            // loop through relationship-list data, to get vserver relations
            logger.debug("Parsing Vserver details from VNF relations");
            for (String ctxKeySet : vnfCtx.getAttributeKeySet()) {
                if (ctxKeySet.startsWith("vnfRetrived.") && "vserver".equalsIgnoreCase(vnfCtx.getAttribute(ctxKeySet))) {
                    String vmKey = ctxKeySet.substring(0, ctxKeySet.length() - "related-to".length());
                    String vserverID = null;
                    String tenantID = null;
                    String cloudOwner = null;
                    String cloudRegionId = null;
                    int relationshipLength = getAttribute(vnfCtx, vmKey, RELATIONSHIP_DATA_LEN_PARAM);

                    for (int j = 0; j < relationshipLength; j++) {
                        // loop inside relationship data, to get vserver-id and tenant-id
                        String key = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
                        String value = vnfCtx.getAttribute(vmKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");

                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
                        if ("vserver.vserver-id".equals(key)) {
                            vserverID = value;
                        }
                        if ("tenant.tenant-id".equals(key)) {
                            tenantID = value;
                        }
                        if ("cloud-region.cloud-owner".equals(key)) {
                            cloudOwner = value;
                        }
                        if ("cloud-region.cloud-region-id".equals(key)) {
                            cloudRegionId = value;
                        }
                    }
                    int relatedPropertyLength = getAttribute(vnfCtx, vmKey, RELATED_TO_PROPERTY_LEN_PARAM);
                    for (int j = 0; j < relatedPropertyLength; j++) {
                        // loop inside related-to-property data, to get vserver-name
                        String key = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-key");
                        String value = vnfCtx.getAttribute(vmKey + "related-to-property[" + j + "].property-value");
                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "]." + key, value);
                    }
                    // Retrive VM relations to find vnfc's VM to VNFC is 1 to 1 relation
                    String vmRetrivalKey = "vserver.vserver-id = '" + vserverID + "' AND tenant.tenant_id = '" + tenantID
                            + "'" + "' AND cloud-region.cloud-owner = '" + cloudOwner
                            + "' AND cloud-region.cloud-region-id = '" + cloudRegionId + "'";
                    Map<String, String> paramsVm = new HashMap<>();
                    paramsVm.put(RESOURCE_TYPE_PARAM, "vserver");
                    paramsVm.put(PREFIX_PARAM, "vmRetrived");
                    paramsVm.put(RESOURCE_KEY_PARAM, vmRetrivalKey);
                    SvcLogicContext vmCtx = getSvcLogicContext();

                    logger.debug("Retrieving VM details from A&AI");
                    getResource(paramsVm, vmCtx);
                    if ((SUCCESS_PARAM).equals(vmCtx.getAttribute(GET_RESOURCE_RESULT))) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("Parsing VNFC details from VM relations");
                        }
                        vmURL = vmCtx.getAttribute("vmRetrived.vserver-selflink");
                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].URL", vmURL);

                        // loop through relationship-list data, to get vnfc relations
                        for (String ctxVnfcKeySet : vmCtx.getAttributeKeySet()) {
                            if (ctxVnfcKeySet.startsWith("vmRetrived.")
                                    && "vnfc".equalsIgnoreCase(vmCtx.getAttribute(ctxVnfcKeySet))) {

                                String vnfcKey = ctxVnfcKeySet.substring(0, ctxVnfcKeySet.length() - "related-to".length());

                                relationshipLength = getAttribute(vmCtx, vnfcKey, RELATIONSHIP_DATA_LEN_PARAM);

                                for (int j = 0; j < relationshipLength; j++) {
                                    // loop through relationship data, to get vnfc name
                                    String key = vmCtx
                                            .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-key");
                                    String value = vmCtx
                                            .getAttribute(vnfcKey + RELATIONSHIP_DATA_STR + j + "].relationship-value");
                                    if ("vnfc.vnfc-name".equalsIgnoreCase(key)) {
                                        vnfHierarchyMap.put(VNFF_VM_STR + vmCount + "].VNFC", value);
                                        vmSet = resolveVmSet(vnfcHierarchyMap, value);
                                        vmSet.add(vmURL);
                                        vnfcHierarchyMap.put(value, vmSet);
                                        break;
                                        // VM to VNFC is 1 to 1 relation, once we got the VNFC name we can break the loop
                                    }
                                }
                            }
                        }
                    } else {
                        ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
                        vnfHierarchyMap.put(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
                        logger.error("Failed in getVnfHierarchy, Error retrieving Vserver details. Error message: "
                                + vmCtx.getAttribute(GET_RESOURCE_RESULT));
                        logger.warn("Incorrect or Incomplete VNF Hierarchy");
                        throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
                    }
                    vmCount++;
                }
            }
            vnfHierarchyMap.put("VNF.VMCount", Integer.toString(vmCount));
            if (vmCount == 0) {
                ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, "VM count is 0");
            }
            // code changes for getting vnfcs hirearchy
            populateVnfcsDetailsinContext(vnfcHierarchyMap, ctx);
            // vnf,vnfcCount
            ctx.setAttribute("VNF.VNFCCount", Integer.toString(vnfcHierarchyMap.size()));
            // code changes for getting vnfcs hirearchy
            ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, SUCCESS_PARAM);
            // Finally set all attributes to ctx
            for (Entry<String, String> entry : vnfHierarchyMap.entrySet()) {
                ctx.setAttribute(entry.getKey(), entry.getValue());
            }
        } else {
            ctx.setAttribute(GET_VNF_HIERARCHY_RESULT_PARAM, "FAILURE");
            ctx.setAttribute(DG_OUTPUT_STATUS_MESSAGE, ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
            logger.error("Failed in getVnfHierarchy, Error retrieving VNF details. Error message: "
                    + ctx.getAttribute(GET_RESOURCE_RESULT));
            logger.warn("Incorrect or Incomplete VNF Hierarchy");
            throw new APPCException(ERROR_RETRIEVING_VNFC_HIERARCHY_PARAM);
        }

        if (logger.isDebugEnabled()) {
            logger.debug("exiting getVnfHierarchy======");
        }
    }

    private void trySetHeatStackIDAttribute(SvcLogicContext ctx, SvcLogicContext vnfCtx) {
        if (vnfCtx.getAttribute("vnfRetrived.heat-stack-id") != null) {
            ctx.setAttribute("VNF.heat-stack-id", vnfCtx.getAttribute("vnfRetrived.heat-stack-id"));
        }
    }

    private Set<String> resolveVmSet(Map<String, Set<String>> vnfcHierarchyMap, String value) {

        Set<String> vmSet = vnfcHierarchyMap.get(value);
        if (vmSet == null) {
            vmSet = new HashSet<>();
        }
        return vmSet;
    }

    private int getAttribute(SvcLogicContext ctx, String key, String param) {
        if (ctx.getAttributeKeySet().contains(key + param)) {
            return Integer.parseInt(ctx.getAttribute(key + param));
        }
        return 0;
    }

    void populateVnfcsDetailsinContext(Map<String, Set<String>> vnfcHierarchyMap, SvcLogicContext ctx)
            throws APPCException {
        SvcLogicContext vnfcCtx = new SvcLogicContext();
        int vnfcCounter = 0;
        for (Entry<String, Set<String>> entry : vnfcHierarchyMap.entrySet()) {
            String vnfcRetrivalKey = "vnfc-name = '" + entry.getKey() + "'";
            Map<String, String> paramsVnfc = new HashMap<>();
            paramsVnfc.put(RESOURCE_TYPE_PARAM, "vnfc");
            paramsVnfc.put(PREFIX_PARAM, "vnfcRetrived");
            paramsVnfc.put(RESOURCE_KEY_PARAM, vnfcRetrivalKey);

            logger.debug("Retrieving VM details from A&AI");
            getResource(paramsVnfc, vnfcCtx);
            if (vnfcCtx.getAttribute(GET_RESOURCE_RESULT).equals(SUCCESS_PARAM)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Parsing VNFC details from VM relations");
                }
                // putting required values in the map
                // vnf.vnfc[vnfcIndex].type
                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].TYPE", vnfcCtx.getAttribute("vnfcRetrived.vnfc-type"));

                // vnf.vnfc[vnfcIndex].name
                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].NAME", vnfcCtx.getAttribute("vnfcRetrived.vnfc-name"));

                // vnf.vnfc[vnfcIndex].vmCount
                Set<String> vmSet = entry.getValue();
                String vmCountinVnfcs = Integer.toString(vmSet.size());
                ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM_COUNT", vmCountinVnfcs);
                int vmCount = 0;
                for (String vmURL : vmSet) {
                    ctx.setAttribute(VNF_VNFC_STR + vnfcCounter + "].VM[" + vmCount++ + "].URL", vmURL);
                }

            }
            vnfcCounter++;
        }
    }

    protected SvcLogicContext getSvcLogicContext() {
        return new SvcLogicContext();
    }
}