aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/controller-usecases/src/main/java/org/onap/policy/drools/apps/controller/usecases/step/Step2.java
blob: 5d80ea5f44dccf81312ed417aa3ecd64a354774b (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2020 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.onap.policy.drools.apps.controller.usecases.step;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.onap.aai.domain.yang.CloudRegion;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.aai.domain.yang.ModelVer;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.aai.domain.yang.Tenant;
import org.onap.policy.aai.AaiCqResponse;
import org.onap.policy.common.utils.coder.StandardCoderObject;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.actor.aai.AaiGetPnfOperation;
import org.onap.policy.controlloop.actor.aai.AaiGetTenantOperation;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.eventmanager.Step;
import org.onap.policy.controlloop.eventmanager.StepContext;
import org.onap.policy.drools.apps.controller.usecases.UsecasesConstants;

/**
 * Steps specific to the usecases controller. The {@link #setProperties()} method is used
 * to load the various properties into the operation, extracting enrichment data where
 * appropriate, and extracting other data from the step's context. For each property,
 * there is a getXxx() method for extracting the value and a loadXxx() method for loading
 * the extracted value into the operation. In addition, the
 * {@link #success(OperationOutcome)} method is responsible for extracting responses from
 * an operation outcome and recording the data in the step's context for use by subsequent
 * steps.
 */
public class Step2 extends Step {
    public static final String TARGET_MODEL_VERSION_ID = "modelVersionId";
    public static final String TARGET_MODEL_CUSTOMIZATION_ID = "modelCustomizationId";
    public static final String TARGET_MODEL_INVARIANT_ID = "modelInvariantId";
    public static final String TARGET_RESOURCE_ID = "resourceID";
    public static final String TARGET_INFO_MSG = "Target Entity IDs";
    public static final String ENRICHMENT_PREFIX = "enrichment/";
    public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
    public static final String RESOURCE_LINK = "resource-link";
    public static final String RESULT_DATA = "result-data";

    private static final Map<String, BiConsumer<Step2, String>> PROPERTY_LOADER;
    private static final Map<String, Consumer<Step2>> PROPERTY_SAVER;

    static {
        /*
         * Populate map for PROPERTY_LOADER.
         */
        Map<String, BiConsumer<Step2, String>> map = new HashMap<>();

        map.put(OperationProperties.AAI_DEFAULT_CLOUD_REGION, Step2::loadCloudRegion);
        map.put(OperationProperties.AAI_DEFAULT_TENANT, Step2::loadTenant);
        map.put(OperationProperties.AAI_PNF, Step2::loadPnf);
        map.put(OperationProperties.AAI_RESOURCE_VNF, Step2::loadResourceVnf);
        map.put(OperationProperties.AAI_SERVICE, Step2::loadService);
        map.put(OperationProperties.AAI_SERVICE_MODEL, Step2::loadServiceModel);
        map.put(OperationProperties.AAI_TARGET_ENTITY, Step2::loadTargetEntity);
        map.put(OperationProperties.AAI_VNF, Step2::loadVnf);
        map.put(OperationProperties.AAI_VNF_MODEL, Step2::loadVnfModel);
        map.put(OperationProperties.AAI_VSERVER_LINK, Step2::loadVserverLink);
        map.put(OperationProperties.DATA_VF_COUNT, Step2::loadVfCount);
        map.put(OperationProperties.EVENT_ADDITIONAL_PARAMS, Step2::loadAdditionalEventParams);
        map.put(OperationProperties.EVENT_PAYLOAD, Step2::loadEventPayload);
        map.put(OperationProperties.OPT_CDS_GRPC_AAI_PROPERTIES, Step2::loadOptCdsGrpcAaiProperties);

        map.put(UsecasesConstants.AAI_DEFAULT_GENERIC_VNF, Step2::loadDefaultGenericVnf);

        PROPERTY_LOADER = Collections.unmodifiableMap(map);


        /*
         * Populate map for PROPERTY_SAVER.
         */
        Map<String, Consumer<Step2>> map2 = new HashMap<>();

        map2.put(OperationProperties.DATA_VF_COUNT, Step2::storeVfCount);

        PROPERTY_SAVER = Collections.unmodifiableMap(map2);
    }


    protected final StepContext stepContext;
    protected final VirtualControlLoopEvent event;

    /**
     * {@code True} if the associated preprocessing steps have been loaded, {@code false}
     * otherwise.
     */
    @Getter
    @Setter
    private boolean preprocessed;

    /**
     * Actions to take to store the Operation's properties back into the context.
     */
    private List<Consumer<Step2>> postProcessors = new LinkedList<>();


    /**
     * Constructs the object. This is used when constructing the step for the policy's
     * actual operation.
     *
     * @param stepContext the step's context
     * @param params operation parameters
     * @param event the event being processed
     */
    public Step2(StepContext stepContext, ControlLoopOperationParams params, VirtualControlLoopEvent event) {
        super(params, new AtomicReference<>());
        this.stepContext = stepContext;
        this.event = event;
    }

    /**
     * Constructs the object using information from another step. This is used when
     * constructing a preprocessing step.
     *
     * @param otherStep step whose information should be used
     * @param actor actor name
     * @param operation operation name
     */
    public Step2(Step2 otherStep, String actor, String operation) {
        super(otherStep, actor, operation);
        this.stepContext = otherStep.stepContext;
        this.event = otherStep.event;
    }

    /**
     * Determines if starting this step indicates acceptance of the event. The default
     * method simply invokes {@link #isPolicyStep()}.
     *
     * @return {@code true} if this step accepts the event, {@code false} if it is still
     *         indeterminate
     */
    public boolean acceptsEvent() {
        return isPolicyStep();
    }

    /**
     * Indicates that the step succeeded with the given outcome. Invoked by the rules. The
     * default method invokes the post processors.
     *
     * @param outcome operation's outcome
     */
    public void success(OperationOutcome outcome) {
        for (Consumer<Step2> proc : postProcessors) {
            proc.accept(this);
        }
    }

    /**
     * Gets the names of the properties required by the operation. The default method just
     * delegates to the operation to identify the properties.
     *
     * @return the names of the properties required by the operation
     */
    public List<String> getPropertyNames() {
        return getOperation().getPropertyNames();
    }

    /**
     * Sets the operation's properties. This is invoked <i>after</i> any preprocessor
     * steps have been performed. It also adds items to {@link #postProcessors}.
     */
    public void setProperties() {
        postProcessors.clear();

        for (String propName : getPropertyNames()) {
            // identify the saver, if any
            Consumer<Step2> saver = PROPERTY_SAVER.get(propName);
            if (saver != null) {
                postProcessors.add(saver);
            }


            // load data
            if (propName.startsWith(ENRICHMENT_PREFIX)) {
                loadEnrichment(propName);
                continue;
            }

            BiConsumer<Step2, String> loader = PROPERTY_LOADER.get(propName);
            if (loader == null) {
                throw new IllegalArgumentException("unknown property " + propName + " needed by " + getActorName() + "."
                                + getOperationName());
            }

            loader.accept(this, propName);
        }
    }

    protected void loadCloudRegion(String propName) {
        getOperation().setProperty(propName, getCloudRegion());
    }

    protected void loadTenant(String propName) {
        getOperation().setProperty(propName, getTenant());
    }

    protected void loadPnf(String propName) {
        getOperation().setProperty(propName, getPnf());
    }

    protected void loadResourceVnf(String propName) {
        getOperation().setProperty(propName, getResourceVnf());
    }

    protected void loadService(String propName) {
        getOperation().setProperty(propName, getService());
    }

    protected void loadServiceModel(String propName) {
        getOperation().setProperty(propName, getServiceModel());
    }

    protected void loadTargetEntity(String propName) {
        getOperation().setProperty(propName, getTargetEntity());
    }

    protected void loadVnf(String propName) {
        getOperation().setProperty(propName, getVnf());
    }

    protected void loadVnfModel(String propName) {
        getOperation().setProperty(propName, getVnfModel());
    }

    protected void loadVserverLink(String propName) {
        getOperation().setProperty(propName, getVserverLink());
    }

    protected void loadVfCount(String propName) {
        getOperation().setProperty(propName, getVfCount());
    }

    protected void loadEnrichment(String propName) {
        getOperation().setProperty(propName, getEnrichment(propName));
    }

    protected void loadAdditionalEventParams(String propName) {
        getOperation().setProperty(propName, getAdditionalEventParams());
    }

    protected void loadEventPayload(String propName) {
        getOperation().setProperty(propName, getEventPayload());
    }

    protected void loadOptCdsGrpcAaiProperties(String propName) {
        // do nothing
    }

    protected void loadDefaultGenericVnf(String propName) {
        getOperation().setProperty(propName, getDefaultGenericVnf());
    }

    protected CloudRegion getCloudRegion() {
        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getDefaultCloudRegion();
    }

    protected Tenant getTenant() {
        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getDefaultTenant();
    }

    protected StandardCoderObject getPnf() {
        return stepContext.getProperty(AaiGetPnfOperation.getKey(getTargetEntity()));
    }

    protected GenericVnf getResourceVnf() {
        verifyNotNull(TARGET_INFO_MSG, params.getTargetEntityIds());

        String resourceId = params.getTargetEntityIds().get(TARGET_RESOURCE_ID);

        verifyNotNull("Target resource ID", resourceId);

        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getGenericVnfByModelInvariantId(resourceId);
    }

    protected ServiceInstance getService() {
        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getServiceInstance();
    }

    protected ModelVer getServiceModel() {
        AaiCqResponse aaicq = getCustomQueryData();
        ServiceInstance service = aaicq.getServiceInstance();
        return aaicq.getModelVerByVersionId(service.getModelVersionId());
    }

    /**
     * The default method assumes there is only one target entity and that it's stored
     * within the step's context.
     */
    protected String getTargetEntity() {
        return stepContext.getProperty(OperationProperties.AAI_TARGET_ENTITY);
    }

    protected GenericVnf getVnf() {
        verifyNotNull(TARGET_INFO_MSG, params.getTargetEntityIds());

        String modelInvariantId = params.getTargetEntityIds().get(TARGET_MODEL_INVARIANT_ID);

        verifyNotNull(TARGET_MODEL_INVARIANT_ID, modelInvariantId);

        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getGenericVnfByVfModuleModelInvariantId(modelInvariantId);
    }

    protected ModelVer getVnfModel() {
        GenericVnf vnf = getVnf();
        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getModelVerByVersionId(vnf.getModelVersionId());
    }

    protected String getVserverLink() {
        String vserver = event.getAai().get(VSERVER_VSERVER_NAME);
        if (StringUtils.isBlank(vserver)) {
            throw new IllegalArgumentException("missing " + VSERVER_VSERVER_NAME + " in enrichment data");
        }

        StandardCoderObject tenant = stepContext.getProperty(AaiGetTenantOperation.getKey(vserver));
        verifyNotNull("tenant data", tenant);

        String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
        verifyNotNull("tenant data resource-link", resourceLink);

        return stripPrefix(resourceLink, 3);
    }

    protected Integer getVfCount() {
        if (stepContext.contains(OperationProperties.DATA_VF_COUNT)) {
            return stepContext.getProperty(OperationProperties.DATA_VF_COUNT);
        }

        verifyNotNull(TARGET_INFO_MSG, params.getTargetEntityIds());

        String modelCustomizationId = params.getTargetEntityIds().get(TARGET_MODEL_CUSTOMIZATION_ID);
        String modelInvariantId = params.getTargetEntityIds().get(TARGET_MODEL_INVARIANT_ID);
        String modelVersionId = params.getTargetEntityIds().get(TARGET_MODEL_VERSION_ID);

        verifyNotNull("target modelCustomizationId", modelCustomizationId);
        verifyNotNull("target modelInvariantId", modelInvariantId);
        verifyNotNull("target modelVersionId", modelVersionId);

        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getVfModuleCount(modelCustomizationId, modelInvariantId, modelVersionId);
    }

    protected String getEnrichment(String propName) {
        String enrichmentKey = propName.substring(ENRICHMENT_PREFIX.length());
        String value = event.getAai().get(enrichmentKey);
        verifyNotNull(propName, value);

        return value;
    }

    protected Map<String, String> getAdditionalEventParams() {
        return event.getAdditionalEventParams();
    }

    protected String getEventPayload() {
        return event.getPayload();
    }

    protected GenericVnf getDefaultGenericVnf() {
        AaiCqResponse aaicq = getCustomQueryData();
        return aaicq.getDefaultGenericVnf();
    }

    protected AaiCqResponse getCustomQueryData() {
        AaiCqResponse aaicq = stepContext.getProperty(AaiCqResponse.CONTEXT_KEY);
        verifyNotNull("custom query data", aaicq);

        return aaicq;
    }

    protected void storeVfCount() {
        if (!getOperation().containsProperty(OperationProperties.DATA_VF_COUNT)) {
            return;
        }

        int vfcount = getOperation().getProperty(OperationProperties.DATA_VF_COUNT);
        stepContext.setProperty(OperationProperties.DATA_VF_COUNT, vfcount);
    }

    protected void verifyNotNull(String propName, Object value) {
        if (value == null) {
            throw new IllegalArgumentException(
                            "missing " + propName + " for " + getActorName() + "." + getOperationName());
        }
    }

    protected static String stripPrefix(String resourceLink, int ncomponents) {
        int previdx = -1;
        for (int nslashes = 0; nslashes < ncomponents; ++nslashes) {
            int idx = resourceLink.indexOf('/', previdx + 1);
            if (idx < 0) {
                break;
            }

            previdx = idx;
        }

        return resourceLink.substring(Math.max(0, previdx));
    }
}