summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/impl/RuleEditorBusinessLogic.java
blob: d17a6bf167d4e12c35a1eb12ebbdc85399607905 (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
package org.onap.sdc.dcae.composition.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.JsonParseException;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.composition.CompositionConfig;
import org.onap.sdc.dcae.composition.restmodels.ruleeditor.*;
import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
import org.onap.sdc.dcae.composition.restmodels.sdc.ResourceDetailed;
import org.onap.sdc.dcae.composition.util.DcaeBeConstants;
import org.onap.sdc.dcae.enums.ArtifactType;
import org.onap.sdc.dcae.errormng.ActionStatus;
import org.onap.sdc.dcae.errormng.ErrConfMgr;
import org.onap.sdc.dcae.errormng.ServiceException;
import org.onap.sdc.dcae.rule.editor.impl.RulesBusinessLogic;
import org.onap.sdc.dcae.rule.editor.utils.RulesPayloadUtils;
import org.onap.sdc.dcae.utils.Normalizers;
import org.onap.sdc.dcae.utils.SdcRestClientUtils;
import org.onap.sdc.dcae.ves.VesDataItemsDefinition;
import org.onap.sdc.dcae.ves.VesDataTypeDefinition;
import org.onap.sdc.dcae.ves.VesSimpleTypesEnum;
import org.onap.sdc.dcae.ves.VesStructureLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class RuleEditorBusinessLogic extends BaseBusinessLogic {

    @Autowired
    private RulesBusinessLogic rulesBusinessLogic;
    @Autowired
    private CompositionConfig compositionConfig;

    private static final String EXCEPTION = "Exception {}";

    public ResponseEntity saveRule(String json, String requestId, String userId, String vfcmtUuid, String dcaeCompLabel, String nid, String configParam) {

        try {
            Rule rule = RulesPayloadUtils.parsePayloadToRule(json);
            if (null == rule) {
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.INVALID_RULE_FORMAT);
            }

            List<ServiceException> errors = rulesBusinessLogic.validateRule(rule);
            if (!errors.isEmpty()) {
                return ErrConfMgr.INSTANCE.buildErrorArrayResponse(errors);
            }

            ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
            checkVfcmtType(vfcmt);

            if (CollectionUtils.isEmpty(vfcmt.getArtifacts())) {
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.SAVE_RULE_FAILED);
            }

            String artifactLabel = Normalizers.normalizeArtifactLabel(dcaeCompLabel + nid + configParam);

            // check for MappingRules artifact in existing artifacts
            Artifact artifactFound = vfcmt.getArtifacts().stream()
                    .filter(a -> artifactLabel.equals(Normalizers.normalizeArtifactLabel(a.getArtifactLabel())))
                    .findAny().orElse(null);

            // exception thrown if vfcmt is checked out and current user is not its owner
            // performs checkoutVfcmt if required
            String vfcmtId = assertOwnershipOfVfcmtId(userId, vfcmt, requestId);
            // new mappingRules artifact, validate nid exists in composition before creating new artifact
            if (null == artifactFound) {
                if (cdumpContainsNid(vfcmt, nid, requestId)) {
                    return saveNewRulesArtifact(rule, vfcmtId, generateMappingRulesFileName(dcaeCompLabel, nid, configParam), artifactLabel, userId, requestId);
                }
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.NODE_NOT_FOUND, "", dcaeCompLabel);
            }

            //update artifact flow - append new rule or edit existing rule
            return addOrEditRuleInArtifact(rule, vfcmtId, userId, artifactFound, requestId);

        } catch (JsonParseException je) {
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error: Rule format is invalid: {}", je);
            return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.INVALID_RULE_FORMAT, "", je.getMessage());
        } catch (Exception e) {
            return ErrConfMgr.INSTANCE.handleException(e, ErrConfMgr.ApiType.SAVE_RULE_ARTIFACT);
        }

    }

    public ResponseEntity getRules(String vfcmtUuid, String dcaeCompLabel, String nid, String configParam, String requestId) {

        try {
            ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
            if (CollectionUtils.isEmpty(vfcmt.getArtifacts())) {
                return new ResponseEntity<>("{}", HttpStatus.OK);
            }
            String artifactLabel = Normalizers.normalizeArtifactLabel(dcaeCompLabel + nid + configParam);

            // check for MappingRules artifact in existing artifacts
            Artifact artifactListed = vfcmt.getArtifacts().stream().filter(a -> artifactLabel.equals(Normalizers.normalizeArtifactLabel(a.getArtifactLabel()))).findAny().orElse(null);
            if (null == artifactListed) {
                return new ResponseEntity<>("{}", HttpStatus.OK);
            }
            String ruleFile = getSdcRestClient().getResourceArtifact(vfcmtUuid, artifactListed.getArtifactUUID(), requestId);

            // To avoid opening the file for reading we search for the eventType and SchemaVer from the artifact metadata's description
            SchemaInfo schemainfo = RulesPayloadUtils.extractInfoFromDescription(artifactListed);
            List<EventTypeDefinitionUI> schema = null == schemainfo ? new ArrayList<>() : getEventTypeDefinitionUIs(schemainfo.getVersion(), schemainfo.getEventType());
            return new ResponseEntity<>(RulesPayloadUtils.buildSchemaAndRulesResponse(ruleFile, schema), HttpStatus.OK);
        } catch (Exception e) {
            return ErrConfMgr.INSTANCE.handleException(e, ErrConfMgr.ApiType.GET_RULE_ARTIFACT);
        }

    }

    public ResponseEntity deleteRule(String userId, String vfcmtUuid, String dcaeCompLabel, String nid, String configParam, String ruleUid, String requestId) {

        try {
            ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
            if (null == vfcmt.getArtifacts()) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "VFCMT {} doesn't have artifacts", vfcmtUuid);
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.DELETE_RULE_FAILED);
            }
            String artifactLabel = Normalizers.normalizeArtifactLabel(dcaeCompLabel + nid + configParam);

            // check for MappingRules artifact in existing artifacts
            Artifact mappingRuleFile = vfcmt.getArtifacts().stream()
                    .filter(a -> artifactLabel.equals(Normalizers.normalizeArtifactLabel(a.getArtifactLabel())))
                    .findAny().orElse(null);

            if (null == mappingRuleFile) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "{} doesn't exist for VFCMT {}", artifactLabel, vfcmtUuid);
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.DELETE_RULE_FAILED);
            }

            String vfcmtId = assertOwnershipOfVfcmtId(userId, vfcmt, requestId);
            String payload = getSdcRestClient().getResourceArtifact(vfcmtId, mappingRuleFile.getArtifactUUID(), requestId);
            MappingRules rules = RulesPayloadUtils.parseMappingRulesArtifactPayload(payload);
            Rule removedRule = rulesBusinessLogic.deleteRule(rules, ruleUid);
            if (null == removedRule) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Rule {} not found.", ruleUid);
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.DELETE_RULE_FAILED);
            }
            if (rules.isEmpty()) { // if file doesn't contain any rules after last deletion -> let's delete the file
                getSdcRestClient().deleteResourceArtifact(userId, vfcmtId, mappingRuleFile.getArtifactUUID(), requestId);
            } else {
                updateRulesArtifact(vfcmtId, userId, mappingRuleFile, rules, requestId);
            }
            return checkInAndReturnSaveArtifactResult(removedRule, vfcmtId, userId, requestId);
        } catch (Exception e) {
            return ErrConfMgr.INSTANCE.handleException(e, ErrConfMgr.ApiType.SAVE_RULE_ARTIFACT);
        }

    }

    public ResponseEntity translateRules(String vfcmtUuid, String requestId, String dcaeCompLabel, String nid, String configParam, String flowType) {

        try {

            if (StringUtils.isBlank(flowType) || MapUtils.isEmpty(compositionConfig.getFlowTypesMap()) || null == compositionConfig.getFlowTypesMap().get(flowType)) {
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.TRANSLATE_FAILED, "", "Flow type " + flowType + " not found");
            }

            // extract entry phase name and last phase name from configuration:
            String entryPointPhaseName = compositionConfig.getFlowTypesMap().get(flowType).getEntryPointPhaseName();
            String lastPhaseName = compositionConfig.getFlowTypesMap().get(flowType).getLastPhaseName();

            ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
            checkVfcmtType(vfcmt);

            if (CollectionUtils.isEmpty(vfcmt.getArtifacts())) {
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.TRANSLATE_FAILED, "", "No rules found on VFCMT " + vfcmtUuid);
            }
            String artifactLabel = Normalizers.normalizeArtifactLabel(dcaeCompLabel + nid + configParam);

            // check for MappingRules artifact in existing artifacts
            Artifact rulesArtifact = vfcmt.getArtifacts().stream().filter(a -> artifactLabel.equals(Normalizers.normalizeArtifactLabel(a.getArtifactLabel()))).findAny().orElse(null);

            if (rulesArtifact == null) {
                return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.TRANSLATE_FAILED, "", artifactLabel + " doesn't exist on VFCMT " + vfcmtUuid);
            }

            String payload = getSdcRestClient().getResourceArtifact(vfcmtUuid, rulesArtifact.getArtifactUUID(), requestId);
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Retrieved mapping rules artifact {}, start parsing rules...", artifactLabel);
            MappingRules rules = RulesPayloadUtils.parseMappingRulesArtifactPayload(payload);
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished parsing rules, calling validator...");
            List<ServiceException> errors = rulesBusinessLogic.validateRules(rules);
            if (!errors.isEmpty()) {
                return ErrConfMgr.INSTANCE.buildErrorArrayResponse(errors);
            }

            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Validation completed successfully, calling translator...");
            String translateJson = rulesBusinessLogic.translateRules(rules, entryPointPhaseName, lastPhaseName, vfcmt.getName());
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Translation completed successfully");
            return new ResponseEntity<>(translateJson, HttpStatus.OK);
        } catch (Exception e) {
            return ErrConfMgr.INSTANCE.handleException(e, ErrConfMgr.ApiType.SAVE_RULE_ARTIFACT);
        }
    }

    public ResponseEntity getExistingRuleTargets(String vfcmtUuid, String requestId, String dcaeCompLabel, String nid) {

        try {
            ResourceDetailed vfcmt = getSdcRestClient().getResource(vfcmtUuid, requestId);
            if (CollectionUtils.isEmpty(vfcmt.getArtifacts())) {
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "getExistingRuleTargets - no rules found! returning zero response.");
                return new ResponseEntity<>("{}", HttpStatus.OK);
            }
            String artifactNameStartWith = dcaeCompLabel + "_" + nid + "_";

            List<String> artifactWithRules = vfcmt.getArtifacts().stream().filter(artifact ->
                    artifact.getArtifactName().startsWith(artifactNameStartWith))
                    .map(artifact -> StringUtils.substringBetween(artifact.getArtifactName(), artifactNameStartWith, DcaeBeConstants.Composition.fileNames.MAPPING_RULE_POSTFIX)).collect(Collectors.toList());
            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "getExistingRuleTargets completed successfully");
            return new ResponseEntity<>(artifactWithRules, HttpStatus.OK);
        } catch (Exception e) {
            debugLogger.log(LogLevel.ERROR, this.getClass().getName(), "getExistingRuleTargets had exception", e);
            return ErrConfMgr.INSTANCE.handleException(e, ErrConfMgr.ApiType.GET_RULE_ARTIFACT);
        }
    }

    public ResponseEntity getDefinition(String version, String eventType) {

        try {
            List<EventTypeDefinitionUI> result = getEventTypeDefinitionUIs(version, eventType);

            return new ResponseEntity<>(result, HttpStatus.OK);

        } catch (Exception e) {
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), EXCEPTION, e);
            return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.VES_SCHEMA_NOT_FOUND);
        }
    }

    public ResponseEntity getEventsByVersion() {
        try {

            Map<String, Set<String>> eventsByVersions = VesStructureLoader.getAvailableVersionsAndEventTypes();

            List<EventTypesByVersionUI> resBody = eventsByVersions.entrySet().stream().map(entry -> {
                Set<String> events = entry.getValue().stream().filter(event -> !EventTypesByVersionUI.DEFAULT_EVENTS.contains(event)).collect(Collectors.toSet());
                return new EventTypesByVersionUI(entry.getKey(), events);
            }).collect(Collectors.toList());

            debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Got a request to return all ves event types by versions {}", eventsByVersions);
            return new ResponseEntity<>(resBody, HttpStatus.OK);

        } catch (Exception e) {
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), EXCEPTION, e);
            return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.VES_SCHEMA_NOT_FOUND);
        }
    }

    ///////////////////PRIVATE METHODS////////////////////////////////////////////////////////////////////////

    private String assertOwnershipOfVfcmtId(String userId, ResourceDetailed vfcmt, String requestId) {
        checkUserIfResourceCheckedOut(userId, vfcmt);
        String newVfcmtId = vfcmt.getUuid(); // may change after checking out a certified vfcmt
        if (isNeedToCheckOut(vfcmt.getLifecycleState())) {
            ResourceDetailed result = checkoutVfcmt(userId, newVfcmtId, requestId);
            if (result != null) {
                newVfcmtId = result.getUuid();
                debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "New resource after checkoutVfcmt is: {}", newVfcmtId);
            }
        }
        return newVfcmtId;
    }

    // called after validating vfcmt.getArtifacts() is not null
    private boolean cdumpContainsNid(ResourceDetailed vfcmt, String nid, String requestId) {
        Artifact cdump = vfcmt.getArtifacts().stream()
                .filter(a -> DcaeBeConstants.Composition.fileNames.COMPOSITION_YML.equalsIgnoreCase(a.getArtifactName()))
                .findAny().orElse(null);
        if (null == cdump || null == cdump.getArtifactUUID()) {
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), "No {} found on vfcmt {}", DcaeBeConstants.Composition.fileNames.COMPOSITION_YML, vfcmt.getUuid());
            return false;
        }
        try {
            String artifact = getSdcRestClient().getResourceArtifact(vfcmt.getUuid(), cdump.getArtifactUUID(), requestId);
            if (!artifact.contains("\"nid\":\"" + nid)) {
                errLogger.log(LogLevel.ERROR, this.getClass().getName(), "{} doesn't contain nid {}. Cannot save mapping rule file", DcaeBeConstants.Composition.fileNames.COMPOSITION_YML, nid);
                return false;
            }
        } catch (Exception e) {
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), EXCEPTION, e);
            return false;
        }
        return true;
    }

    private ResponseEntity<String> saveNewRulesArtifact(Rule rule, String vfcmtUuid, String artifactFileName, String artifactLabel, String userId, String requestId) throws JsonProcessingException {
        MappingRules body = new MappingRules(rule);
        Artifact artifact = SdcRestClientUtils.generateDeploymentArtifact(body.describe(), artifactFileName, ArtifactType.OTHER.name(), artifactLabel, body.convertToPayload());
        getSdcRestClient().createResourceArtifact(userId, vfcmtUuid, artifact, requestId);
        return checkInAndReturnSaveArtifactResult(rule, vfcmtUuid, userId, requestId);
    }

    private ResponseEntity addOrEditRuleInArtifact(Rule rule, String vfcmtUuid, String userId, Artifact rulesArtifact, String requestId) throws JsonProcessingException {
        String payload = getSdcRestClient().getResourceArtifact(vfcmtUuid, rulesArtifact.getArtifactUUID(), requestId);
        MappingRules rules = RulesPayloadUtils.parseMappingRulesArtifactPayload(payload);

        // in case the rule id is passed but the rule doesn't exist on the mapping rule file:
        if (!rulesBusinessLogic.addOrEditRule(rules, rule)) {
            return ErrConfMgr.INSTANCE.buildErrorResponse(ActionStatus.SAVE_RULE_FAILED);
        }
        updateRulesArtifact(vfcmtUuid, userId, rulesArtifact, rules, requestId);
        return checkInAndReturnSaveArtifactResult(rule, vfcmtUuid, userId, requestId);
    }

    // regardless of check in result, return save artifact success
    private ResponseEntity<String> checkInAndReturnSaveArtifactResult(Rule rule, String vfcmtUuid, String userId, String requestId) {
        try {
            checkinVfcmt(userId, vfcmtUuid, requestId);
        } catch (Exception e) {
            // swallowing the exception intentionally since it is on the check in action
            errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error occurred while performing check in on VFCMT {}:{}", vfcmtUuid, e);
        }
        return new ResponseEntity<>(rule.toJson(), HttpStatus.OK);
    }

    private void updateRulesArtifact(String vfcmtUuid, String userId, Artifact artifactInfo, MappingRules rules, String requestId) throws JsonProcessingException {
        artifactInfo.setPayloadData(Base64Utils.encodeToString(rules.convertToPayload()));
        // POST must contain 'description' while GET returns 'artifactDescription'
        artifactInfo.setDescription(artifactInfo.getArtifactDescription());
        getSdcRestClient().updateResourceArtifact(userId, vfcmtUuid, artifactInfo, requestId);
    }


    /**
     * @param eventMapStream
     * @param parent
     * @param path
     * @return
     */
    private List<EventTypeDefinitionUI> convertToEventTypeDefinition(Stream<Map.Entry<String, VesDataTypeDefinition>> eventMapStream, VesDataTypeDefinition parent, String path) {

        return eventMapStream.map(entry -> {
            Map<String, VesDataTypeDefinition> properties = entry.getValue().getProperties();
            VesDataItemsDefinition items = entry.getValue().getItems();
            String newPath = path + "." + entry.getKey();
            List<EventTypeDefinitionUI> children = (properties == null) ? null : convertToEventTypeDefinition(properties.entrySet().stream(), entry.getValue(), newPath);
            if (VesSimpleTypesEnum.ARRAY.getType().equals(entry.getValue().getType())) {
                newPath += "[]";
                if (innerTypeIsComplex(items)) {
                    children = convertComplexArrayType(items, newPath);
                } else if (innerTypeIsArray(items)) {
                    newPath += "[]";
                }
            }

            boolean isRequired = (parent != null) ? parent.getRequired().contains(entry.getKey()) : false;
            return new EventTypeDefinitionUI(entry.getKey(), children, isRequired, newPath);
        }).collect(Collectors.toList());
    }

    private boolean innerTypeIsComplex(VesDataItemsDefinition items) {
        return items != null && items.stream().anyMatch(p -> p.getProperties() != null);
    }

    private boolean innerTypeIsArray(VesDataItemsDefinition items) {
        return items != null && items.stream().anyMatch(p -> p.getItems() != null);
    }

    private List<EventTypeDefinitionUI> convertComplexArrayType(VesDataItemsDefinition items, String path) {
        return items.stream().map(item -> item.getProperties() != null ? convertToEventTypeDefinition(item.getProperties().entrySet().stream(), item, path) : new ArrayList<EventTypeDefinitionUI>())
                .flatMap(List::stream).collect(Collectors.toList());
    }


    private String generateMappingRulesFileName(String dcaeCompLabel, String nid, String configParam) {
        return dcaeCompLabel + "_" + nid + "_" + configParam + DcaeBeConstants.Composition.fileNames.MAPPING_RULE_POSTFIX;
    }

    private List<EventTypeDefinitionUI> getEventTypeDefinitionUIs(String version, String eventType) {
        List<String> eventNamesToReturn = ListUtils.union(EventTypesByVersionUI.DEFAULT_EVENTS, Arrays.asList(eventType));
        Map<String, VesDataTypeDefinition> eventDefs = VesStructureLoader.getEventListenerDefinitionByVersion(version);
        Stream<Map.Entry<String, VesDataTypeDefinition>> filteredEvents = eventDefs.entrySet().stream().filter(entry -> eventNamesToReturn.contains(entry.getKey()));

        return convertToEventTypeDefinition(filteredEvents, null, "event");
    }
}