16351b8207f970db667ad2dd34ccb94d9ca554dc
[clamp.git] / src / main / java / org / onap / clamp / loop / CsarInstaller.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
29
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map.Entry;
35
36 import org.json.simple.parser.ParseException;
37 import org.onap.clamp.clds.client.DcaeInventoryServices;
38 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
39 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
40 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
41 import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser;
42 import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator;
43 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
44 import org.onap.clamp.clds.sdc.controller.installer.MicroService;
45 import org.onap.clamp.clds.util.JsonUtils;
46 import org.onap.clamp.clds.util.drawing.SvgFacade;
47 import org.onap.clamp.loop.deploy.DcaeDeployParameters;
48 import org.onap.clamp.loop.service.Service;
49 import org.onap.clamp.loop.service.ServiceRepository;
50 import org.onap.clamp.policy.Policy;
51 import org.onap.clamp.policy.microservice.MicroServicePolicy;
52 import org.onap.clamp.policy.operational.OperationalPolicy;
53 import org.onap.sdc.tosca.parser.api.IEntityDetails;
54 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
55 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
56 import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
57 import org.onap.sdc.tosca.parser.enums.SdcTypes;
58 import org.onap.sdc.toscaparser.api.NodeTemplate;
59 import org.onap.sdc.toscaparser.api.Property;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.beans.factory.annotation.Qualifier;
62 import org.springframework.stereotype.Component;
63 import org.springframework.transaction.annotation.Propagation;
64 import org.springframework.transaction.annotation.Transactional;
65 import org.yaml.snakeyaml.Yaml;
66
67 /**
68  * This class will be instantiated by spring config, and used by Sdc Controller.
69  * There is no state kept by the bean. It's used to deploy the csar/notification
70  * received from SDC in DB.
71  */
72 @Component
73 @Qualifier("csarInstaller")
74 public class CsarInstaller {
75
76     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstaller.class);
77     public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
78     public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
79     // This will be used later as the policy scope
80     public static final String MODEL_NAME_PREFIX = "Loop_";
81
82     @Autowired
83     LoopsRepository loopRepository;
84
85     @Autowired
86     ServiceRepository serviceRepository;
87
88     @Autowired
89     BlueprintParser blueprintParser;
90
91     @Autowired
92     ChainGenerator chainGenerator;
93
94     @Autowired
95     DcaeInventoryServices dcaeInventoryService;
96
97     @Autowired
98     private SvgFacade svgFacade;
99
100     /**
101     * Verify whether Csar is deployed.
102     * 
103     * @param csar The Csar Handler
104     * @return The flag indicating whether Csar is deployed
105     * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
106     */
107     public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
108         boolean alreadyInstalled = true;
109         JsonObject serviceDetails = JsonUtils.GSON.fromJson(
110                 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
111         alreadyInstalled = alreadyInstalled
112                 && serviceRepository.existsById(serviceDetails.get("UUID").getAsString());
113
114         for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
115             alreadyInstalled = alreadyInstalled
116                     && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
117                             csar.getSdcNotification().getServiceVersion(),
118                             blueprint.getValue().getResourceAttached().getResourceInstanceName(),
119                             blueprint.getValue().getBlueprintArtifactName()));
120         }
121         
122         return alreadyInstalled;
123     }
124
125     /**
126      * Install the service and loops from the csar.
127      * 
128      * @param csar The Csar Handler
129      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
130      * @throws InterruptedException The InterruptedException
131      */
132     public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException {
133         logger.info("Installing the CSAR " + csar.getFilePath());
134         installTheLoop(csar, installTheService(csar));
135         logger.info("Successfully installed the CSAR " + csar.getFilePath());
136     }
137
138     /**
139      * Install the Loop from the csar.
140      * 
141      * @param csar The Csar Handler
142      * @param service The service object that is related to the loop
143      * @throws SdcArtifactInstallerException The SdcArtifactInstallerException
144      * @throws InterruptedException The InterruptedException
145      */
146     @Transactional(propagation = Propagation.REQUIRED)
147     public void installTheLoop(CsarHandler csar, Service service) 
148             throws SdcArtifactInstallerException, InterruptedException {
149         try {
150             logger.info("Installing the Loops");
151             for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
152                 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
153                 loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue(), service));
154             }
155             logger.info("Successfully installed the Loops ");
156         } catch (IOException e) {
157             throw new SdcArtifactInstallerException("Exception caught during the Loop installation in database", e);
158         } catch (ParseException e) {
159             throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
160         }
161     }
162
163     /**
164      * Install the Service from the csar.
165      * 
166      * @param csar The Csar Handler
167      * @return The service object
168      */
169     @Transactional
170     public Service installTheService(CsarHandler csar) {
171         logger.info("Start to install the Service from csar");
172         JsonObject serviceDetails = JsonUtils.GSON.fromJson(
173                 JsonUtils.GSON.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class);
174
175         // Add properties details for each type, VfModule, VF, VFC, ....
176         JsonObject resourcesProp = createServicePropertiesByType(csar);
177         resourcesProp.add("VFModule", createVfModuleProperties(csar));
178
179         Service modelService = new Service(serviceDetails, resourcesProp, 
180                 csar.getSdcNotification().getServiceVersion());
181
182         serviceRepository.save(modelService);
183         logger.info("Successfully installed the Service");
184         return modelService;
185     }
186
187     private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact, Service service)
188             throws IOException, ParseException, InterruptedException {
189         Loop newLoop = new Loop();
190         newLoop.setBlueprint(blueprintArtifact.getDcaeBlueprint());
191         newLoop.setName(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
192                 csar.getSdcNotification().getServiceVersion(),
193                 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
194                 blueprintArtifact.getBlueprintArtifactName()));
195         newLoop.setLastComputedState(LoopState.DESIGN);
196
197         List<MicroService> microServicesChain = chainGenerator
198                 .getChainOfMicroServices(blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint()));
199         if (microServicesChain.isEmpty()) {
200             microServicesChain = blueprintParser.fallbackToOneMicroService(blueprintArtifact.getDcaeBlueprint());
201         }
202         newLoop.setModelService(service);
203         newLoop.setMicroServicePolicies(
204                 createMicroServicePolicies(microServicesChain, csar, blueprintArtifact, newLoop));
205         newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
206
207         newLoop.setSvgRepresentation(svgFacade.getSvgImage(microServicesChain));
208         newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(blueprintArtifact, newLoop));
209
210         DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
211         newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
212         return newLoop;
213     }
214
215     private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
216             Loop newLoop) {
217         return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
218                 csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
219                 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
220                 blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
221     }
222
223     private HashSet<MicroServicePolicy> createMicroServicePolicies(List<MicroService> microServicesChain,
224             CsarHandler csar, BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
225         HashSet<MicroServicePolicy> newSet = new HashSet<>();
226
227         for (MicroService microService : microServicesChain) {
228             MicroServicePolicy microServicePolicy = new MicroServicePolicy(
229                     Policy.generatePolicyName(microService.getName(), csar.getSdcNotification().getServiceName(),
230                             csar.getSdcNotification().getServiceVersion(),
231                             blueprintArtifact.getResourceAttached().getResourceInstanceName(),
232                             blueprintArtifact.getBlueprintArtifactName()),
233                     microService.getModelType(), csar.getPolicyModelYaml().orElse(""), false,
234                     new HashSet<>(Arrays.asList(newLoop)));
235
236             newSet.add(microServicePolicy);
237             microService.setMappedNameJpa(microServicePolicy.getName());
238         }
239         return newSet;
240     }
241
242     private JsonObject createGlobalPropertiesJson(BlueprintArtifact blueprintArtifact, Loop newLoop) {
243         return DcaeDeployParameters.getDcaeDeploymentParametersInJson(blueprintArtifact, newLoop);
244     }
245
246     private static JsonObject createVfModuleProperties(CsarHandler csar) {
247         JsonObject vfModuleProps = new JsonObject();
248         // Loop on all Groups defined in the service (VFModule entries type:
249         // org.openecomp.groups.VfModule)
250         for (IEntityDetails entity : csar.getSdcCsarHelper().getEntity(
251                 EntityQuery.newBuilder(EntityTemplateType.GROUP).build(),
252                 TopologyTemplateQuery.newBuilder(SdcTypes.SERVICE).build(), false)) {
253             // Get all metadata info
254             JsonObject allVfProps = (JsonObject) JsonUtils.GSON.toJsonTree(entity.getMetadata().getAllProperties());
255             vfModuleProps.add(entity.getMetadata().getAllProperties().get("vfModuleModelName"), allVfProps);
256             // now append the properties section so that we can also have isBase,
257             // volume_group, etc ... fields under the VFmodule name
258             for (Entry<String, Property> additionalProp : entity.getProperties().entrySet()) {
259                 allVfProps.add(additionalProp.getValue().getName(),
260                         JsonUtils.GSON.toJsonTree(additionalProp.getValue().getValue()));
261             }
262         }
263         return vfModuleProps;
264     }
265
266     private static JsonObject createServicePropertiesByType(CsarHandler csar) {
267         JsonObject resourcesProp = new JsonObject();
268         // Iterate on all types defined in the tosca lib
269         for (SdcTypes type : SdcTypes.values()) {
270             JsonObject resourcesPropByType = new JsonObject();
271             // For each type, get the metadata of each nodetemplate
272             for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
273                 resourcesPropByType.add(nodeTemplate.getName(),
274                         JsonUtils.GSON.toJsonTree(nodeTemplate.getMetaData().getAllProperties()));
275             }
276             resourcesProp.add(type.getValue(), resourcesPropByType);
277         }
278         return resourcesProp;
279     }
280
281     /**
282      * ll get the latest version of the artifact (version can be specified to DCAE
283      * call).
284      *
285      * @return The DcaeInventoryResponse object containing the dcae values
286      */
287     private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
288             throws IOException, ParseException, InterruptedException {
289         return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
290                 blueprintArtifact.getBlueprintInvariantServiceUuid(),
291                 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
292     }
293
294 }