aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/loop/service/CsarServiceInstaller.java
blob: 6db6d920fe34e5f80b31492377ad1b01d5921491 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights
 *                             reserved.
 * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
 * ================================================================================
 * 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.clamp.loop.service;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.google.gson.JsonObject;
import java.util.Map.Entry;
import org.onap.clamp.clds.client.CdsServices;
import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;
import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
import org.onap.clamp.clds.util.JsonUtils;
import org.onap.sdc.tosca.parser.api.IEntityDetails;
import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
import org.onap.sdc.tosca.parser.enums.SdcTypes;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component
@Qualifier("csarInstaller")
public class CsarServiceInstaller {
    private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarServiceInstaller.class);

    @Autowired
    ServicesRepository serviceRepository;

    @Autowired
    CdsServices cdsServices;

    /**
     * Install the Service from the csar.
     *
     * @param csar The Csar Handler
     * @return The service object
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Service installTheService(CsarHandler csar) {
        logger.info("Start to install the Service from csar");
        JsonObject serviceDetails = JsonUtils.GSON.fromJson(
                JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);

        // Add properties details for each type, VfModule, VF, VFC, ....
        JsonObject resourcesProp = createServicePropertiesByType(csar);
        resourcesProp.add("VFModule", createVfModuleProperties(csar));

        Service modelService = new Service(serviceDetails, resourcesProp,
                csar.getSdcNotification().getServiceVersion());

        serviceRepository.save(modelService);
        logger.info("Successfully installed the Service");
        return modelService;
    }

    private JsonObject createServicePropertiesByType(CsarHandler csar) {
        JsonObject resourcesProp = new JsonObject();
        // Iterate on all types defined in the tosca lib
        for (SdcTypes type : SdcTypes.values()) {
            JsonObject resourcesPropByType = new JsonObject();
            // For each type, get the metadata of each nodetemplate
            for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
                resourcesPropByType.add(nodeTemplate.getName(),
                        JsonUtils.GSON.toJsonTree(nodeTemplate.getMetaData().getAllProperties()));
                // get cds artifact information and save in resources Prop
                if (SdcTypes.PNF == type || SdcTypes.VF == type) {
                    JsonObject controllerProperties = createCdsArtifactProperties(nodeTemplate);
                    if (controllerProperties != null) {
                        resourcesPropByType.getAsJsonObject(nodeTemplate.getName())
                                .add("controllerProperties", controllerProperties);
                    }
                }
            }
            resourcesProp.add(type.getValue(), resourcesPropByType);
        }
        return resourcesProp;
    }

    private static JsonObject createVfModuleProperties(CsarHandler csar) {
        JsonObject vfModuleProps = new JsonObject();
        // Loop on all Groups defined in the service (VFModule entries type:
        // org.openecomp.groups.VfModule)
        for (IEntityDetails entity : csar.getSdcCsarHelper().getEntity(
                EntityQuery.newBuilder(EntityTemplateType.GROUP).build(),
                TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false)) {
            // Get all metadata info
            JsonObject allVfProps = (JsonObject) JsonUtils.GSON.toJsonTree(entity.getMetadata().getAllProperties());
            vfModuleProps.add(entity.getMetadata().getAllProperties().get("vfModuleModelName"), allVfProps);
            // now append the properties section so that we can also have isBase,
            // volume_group, etc ... fields under the VFmodule name
            for (Entry<String, Property> additionalProp : entity.getProperties().entrySet()) {
                allVfProps.add(additionalProp.getValue().getName(),
                        JsonUtils.GSON.toJsonTree(additionalProp.getValue().getValue()));
            }
        }
        return vfModuleProps;
    }

    /**
     * Verify whether Service in Csar is deployed.
     *
     * @param csar The Csar Handler
     * @return The flag indicating whether Service is deployed
     * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
     */
    public boolean isServiceAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
        boolean alreadyInstalled = true;
        JsonObject serviceDetails = JsonUtils.GSON.fromJson(
                JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
        alreadyInstalled = serviceRepository.existsById(serviceDetails.get("UUID").getAsString());

        return alreadyInstalled;
    }

    /**
     * Retrive CDS artifacts information from node template and save in resource object.
     *
     * @param nodeTemplate node template
     * @return Returns CDS artifacts information
     */
    private JsonObject createCdsArtifactProperties(NodeTemplate nodeTemplate) {
        Object artifactName = nodeTemplate.getPropertyValue("sdnc_model_name");
        Object artifactVersion = nodeTemplate.getPropertyValue("sdnc_model_version");
        if (artifactName != null && artifactVersion != null) {
            CdsBpWorkFlowListResponse response =
                    queryCdsToGetWorkFlowList(artifactName.toString(), artifactVersion.toString());
            if (response == null) {
                return null;
            }

            JsonObject workFlowProps = new JsonObject();
            for (String workFlow : response.getWorkflows()) {
                JsonObject inputs = queryCdsToGetWorkFlowInputProperties(response.getBlueprintName(),
                        response.getVersion(), workFlow);
                workFlowProps.add(workFlow, inputs);
            }

            JsonObject controllerProperties = new JsonObject();
            controllerProperties.addProperty("sdnc_model_name", artifactName.toString());
            controllerProperties.addProperty("sdnc_model_version", artifactVersion.toString());
            controllerProperties.add("workflows", workFlowProps);
            return controllerProperties;
        }
        return null;
    }

    private CdsBpWorkFlowListResponse queryCdsToGetWorkFlowList(String artifactName, String artifactVersion) {
        return cdsServices.getBlueprintWorkflowList(artifactName, artifactVersion);
    }

    private JsonObject queryCdsToGetWorkFlowInputProperties(String artifactName, String artifactVersion,
                                                            String workFlow) {
        return cdsServices.getWorkflowInputProperties(artifactName, artifactVersion, workFlow);
    }
}