2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.Gson;
29 import com.google.gson.JsonObject;
31 import java.io.IOException;
32 import java.util.Arrays;
33 import java.util.HashSet;
35 import java.util.Map.Entry;
37 import org.json.simple.parser.ParseException;
38 import org.onap.clamp.clds.client.DcaeInventoryServices;
39 import org.onap.clamp.clds.exception.policy.PolicyModelException;
40 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
41 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
42 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
43 import org.onap.clamp.clds.sdc.controller.installer.BlueprintParser;
44 import org.onap.clamp.clds.sdc.controller.installer.ChainGenerator;
45 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
46 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
47 import org.onap.clamp.clds.sdc.controller.installer.MicroService;
48 import org.onap.clamp.clds.util.JsonUtils;
49 import org.onap.clamp.policy.Policy;
50 import org.onap.clamp.policy.microservice.MicroServicePolicy;
51 import org.onap.clamp.policy.operational.OperationalPolicy;
52 import org.onap.sdc.tosca.parser.enums.SdcTypes;
53 import org.onap.sdc.toscaparser.api.NodeTemplate;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.transaction.annotation.Propagation;
56 import org.springframework.transaction.annotation.Transactional;
57 import org.yaml.snakeyaml.Yaml;
60 * This class will be instantiated by spring config, and used by Sdc Controller.
61 * There is no state kept by the bean. It's used to deploy the csar/notification
62 * received from SDC in DB.
64 public class CsarInstallerImpl implements CsarInstaller {
66 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CsarInstallerImpl.class);
67 public static final String TEMPLATE_NAME_PREFIX = "DCAE-Designer-Template-";
68 public static final String CONTROL_NAME_PREFIX = "ClosedLoop-";
69 public static final String GET_INPUT_BLUEPRINT_PARAM = "get_input";
70 // This will be used later as the policy scope
71 public static final String MODEL_NAME_PREFIX = "Loop_";
74 LoopsRepository loopRepository;
77 BlueprintParser blueprintParser;
80 ChainGenerator chainGenerator;
83 DcaeInventoryServices dcaeInventoryService;
86 public boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException {
87 boolean alreadyInstalled = true;
88 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
89 alreadyInstalled = alreadyInstalled
90 && loopRepository.existsById(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
91 csar.getSdcNotification().getServiceVersion(),
92 blueprint.getValue().getResourceAttached().getResourceInstanceName(),
93 blueprint.getValue().getBlueprintArtifactName()));
95 return alreadyInstalled;
99 @Transactional(propagation = Propagation.REQUIRED)
100 public void installTheCsar(CsarHandler csar)
101 throws SdcArtifactInstallerException, InterruptedException, PolicyModelException {
103 logger.info("Installing the CSAR " + csar.getFilePath());
104 for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
105 logger.info("Processing blueprint " + blueprint.getValue().getBlueprintArtifactName());
106 loopRepository.save(createLoopFromBlueprint(csar, blueprint.getValue()));
108 logger.info("Successfully installed the CSAR " + csar.getFilePath());
109 } catch (IOException e) {
110 throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e);
111 } catch (ParseException e) {
112 throw new SdcArtifactInstallerException("Exception caught during the Dcae query to get ServiceTypeId", e);
116 private Loop createLoopFromBlueprint(CsarHandler csar, BlueprintArtifact blueprintArtifact)
117 throws IOException, ParseException, InterruptedException {
118 Loop newLoop = new Loop();
119 newLoop.setBlueprint(blueprintArtifact.getDcaeBlueprint());
120 newLoop.setName(Loop.generateLoopName(csar.getSdcNotification().getServiceName(),
121 csar.getSdcNotification().getServiceVersion(),
122 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
123 blueprintArtifact.getBlueprintArtifactName()));
124 newLoop.setLastComputedState(LoopState.DESIGN);
125 newLoop.setMicroServicePolicies(createMicroServicePolicies(csar, blueprintArtifact, newLoop));
126 newLoop.setOperationalPolicies(createOperationalPolicies(csar, blueprintArtifact, newLoop));
127 // Set SVG XML computed
128 // newLoop.setSvgRepresentation(svgRepresentation);
129 newLoop.setGlobalPropertiesJson(createGlobalPropertiesJson(csar, blueprintArtifact));
130 newLoop.setModelPropertiesJson(createModelPropertiesJson(csar, blueprintArtifact));
131 DcaeInventoryResponse dcaeResponse = queryDcaeToGetServiceTypeId(blueprintArtifact);
132 newLoop.setDcaeBlueprintId(dcaeResponse.getTypeId());
136 private HashSet<OperationalPolicy> createOperationalPolicies(CsarHandler csar, BlueprintArtifact blueprintArtifact,
138 return new HashSet<>(Arrays.asList(new OperationalPolicy(Policy.generatePolicyName("OPERATIONAL",
139 csar.getSdcNotification().getServiceName(), csar.getSdcNotification().getServiceVersion(),
140 blueprintArtifact.getResourceAttached().getResourceInstanceName(),
141 blueprintArtifact.getBlueprintArtifactName()), newLoop, new JsonObject())));
144 private HashSet<MicroServicePolicy> createMicroServicePolicies(CsarHandler csar,
145 BlueprintArtifact blueprintArtifact, Loop newLoop) throws IOException {
146 HashSet<MicroServicePolicy> newSet = new HashSet<>();
147 for (MicroService microService : blueprintParser.getMicroServices(blueprintArtifact.getDcaeBlueprint())) {
148 newSet.add(new MicroServicePolicy(microService.getName(), csar.getPolicyModelYaml().orElse(""), false,
149 new HashSet<>(Arrays.asList(newLoop))));
154 private JsonObject createGlobalPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
155 JsonObject globalProperties = new JsonObject();
156 globalProperties.add("dcaeDeployParameters", getAllBlueprintParametersInJson(blueprintArtifact));
157 return globalProperties;
161 private JsonObject createModelPropertiesJson(CsarHandler csar, BlueprintArtifact blueprintArtifact) {
162 JsonObject modelProperties = new JsonObject();
163 Gson gson = new Gson();
164 modelProperties.add("serviceDetails",
165 gson.fromJson(gson.toJson(csar.getSdcCsarHelper().getServiceMetadataAllProperties()), JsonObject.class));
167 JsonObject resourcesProp = new JsonObject();
168 for (SdcTypes type : SdcTypes.values()) {
169 JsonObject resourcesPropByType = new JsonObject();
170 for (NodeTemplate nodeTemplate : csar.getSdcCsarHelper().getServiceNodeTemplateBySdcType(type)) {
171 resourcesPropByType.add(nodeTemplate.getName(), JsonUtils.GSON_JPA_MODEL
172 .fromJson(new Gson().toJson(nodeTemplate.getMetaData().getAllProperties()), JsonObject.class));
174 resourcesProp.add(type.getValue(), resourcesPropByType);
176 modelProperties.add("resourceDetails", resourcesProp);
177 return modelProperties;
180 private JsonObject getAllBlueprintParametersInJson(BlueprintArtifact blueprintArtifact) {
181 JsonObject node = new JsonObject();
182 Yaml yaml = new Yaml();
183 Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
184 .load(blueprintArtifact.getDcaeBlueprint())).get("inputs"));
185 inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
186 Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
187 if (defaultValue != null) {
188 addPropertyToNode(node, elem.getKey(), defaultValue);
190 node.addProperty(elem.getKey(), "");
193 node.addProperty("policy_id", "AUTO_GENERATED_POLICY_ID_AT_SUBMIT");
198 * ll get the latest version of the artifact (version can be specified to DCAE
201 * @return The DcaeInventoryResponse object containing the dcae values
203 private DcaeInventoryResponse queryDcaeToGetServiceTypeId(BlueprintArtifact blueprintArtifact)
204 throws IOException, ParseException, InterruptedException {
205 return dcaeInventoryService.getDcaeInformation(blueprintArtifact.getBlueprintArtifactName(),
206 blueprintArtifact.getBlueprintInvariantServiceUuid(),
207 blueprintArtifact.getResourceAttached().getResourceInvariantUUID());
210 private void addPropertyToNode(JsonObject node, String key, Object value) {
211 if (value instanceof String) {
212 node.addProperty(key, (String) value);
213 } else if (value instanceof Number) {
214 node.addProperty(key, (Number) value);
215 } else if (value instanceof Boolean) {
216 node.addProperty(key, (Boolean) value);
217 } else if (value instanceof Character) {
218 node.addProperty(key, (Character) value);
220 node.addProperty(key, JsonUtils.GSON.toJson(value));