2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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=========================================================
22 package org.onap.aai.babel.parser;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.util.LinkedList;
28 import java.util.List;
30 import java.util.Properties;
31 import java.util.stream.Collectors;
32 import org.onap.aai.babel.logging.LogHelper;
33 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
34 import org.onap.aai.babel.xml.generator.model.AllotedResource;
35 import org.onap.aai.babel.xml.generator.model.L3NetworkWidget;
36 import org.onap.aai.babel.xml.generator.model.Model;
37 import org.onap.aai.babel.xml.generator.model.ProvidingService;
38 import org.onap.aai.babel.xml.generator.model.Resource;
39 import org.onap.aai.babel.xml.generator.model.Service;
40 import org.onap.aai.babel.xml.generator.model.TunnelXconnectWidget;
41 import org.onap.aai.babel.xml.generator.model.VfModule;
42 import org.onap.aai.babel.xml.generator.model.Widget;
43 import org.onap.aai.babel.xml.generator.types.ModelType;
44 import org.onap.aai.cl.api.Logger;
45 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
46 import org.onap.sdc.toscaparser.api.Group;
47 import org.onap.sdc.toscaparser.api.NodeTemplate;
48 import org.onap.sdc.toscaparser.api.Property;
50 public class ArtifactGeneratorToscaParser {
52 private static Logger log = LogHelper.INSTANCE;
54 public static final String PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE = "artifactgenerator.config";
56 private static final String GENERATOR_AAI_CONFIGFILE_NOT_FOUND =
57 "Cannot generate artifacts. Artifact Generator Configuration file not found at %s";
58 private static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND =
59 "Cannot generate artifacts. artifactgenerator.config system property not configured";
60 private static final String GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING =
61 "Cannot generate artifacts. Providing Service Metadata is missing for allotted resource %s";
62 private static final String GENERATOR_AAI_PROVIDING_SERVICE_MISSING =
63 "Cannot generate artifacts. Providing Service is missing for allotted resource %s";
65 // Metadata properties
66 private static final String CATEGORY = "category";
67 private static final String ALLOTTED_RESOURCE = "Allotted Resource";
68 private static final String SUBCATEGORY = "subcategory";
69 private static final String TUNNEL_XCONNECT = "Tunnel XConnect";
71 private static final String VERSION = "version";
73 private ISdcCsarHelper csarHelper;
76 * Constructs using csarHelper
78 * @param csarHelper The csar helper
80 public ArtifactGeneratorToscaParser(ISdcCsarHelper csarHelper) {
81 this.csarHelper = csarHelper;
85 * Returns the artifact description
87 * @param model the artifact model
88 * @return the artifact model's description
90 public static String getArtifactDescription(Model model) {
91 String artifactDesc = model.getModelDescription();
92 if (model.getModelType().equals(ModelType.SERVICE)) {
93 artifactDesc = "AAI Service Model";
94 } else if (model.getModelType().equals(ModelType.RESOURCE)) {
95 artifactDesc = "AAI Resource Model";
101 * Initialises the widget configuration.
103 * @throws IOException
105 public static void initWidgetConfiguration() throws IOException {
106 log.debug("Getting Widget Configuration");
107 String configLocation = System.getProperty(PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
108 if (configLocation != null) {
109 File file = new File(configLocation);
111 Properties properties = new Properties();
112 properties.load(new FileInputStream(file));
113 WidgetConfigurationUtil.setConfig(properties);
115 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
118 throw new IllegalArgumentException(GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND);
123 * Generates a Resource List using input Service Node Templates.
125 * @param serviceNodes input Service Node Templates
126 * @param idTypeStore ID->Type mapping
127 * @return the processed resource models
129 public List<Resource> processResourceToscas(List<NodeTemplate> serviceNodes, Map<String, String> idTypeStore) {
130 List<Resource> resources = new LinkedList<>();
131 for (NodeTemplate serviceNode : serviceNodes) {
132 List<NodeTemplate> resourceNodes = csarHelper.getNodeTemplateChildren(serviceNode);
134 String resourceUuId = serviceNode.getMetaData().getValue("UUID");
135 String mapValue = idTypeStore.get(resourceUuId);
136 if (mapValue != null) {
137 Model model = Model.getModelFor(idTypeStore.get(serviceNode.getMetaData().getValue("UUID")));
139 log.debug("Inside Resource artifact generation for resource");
140 Map<String, String> serviceMetadata = serviceNode.getMetaData().getAllProperties();
141 model.populateModelIdentificationInformation(serviceMetadata);
143 // Found model from the type store so removing the same
144 idTypeStore.remove(model.getModelNameVersionId());
145 processVfTosca(idTypeStore, model, resourceNodes);
147 // Process group information from tosca for vfModules
148 if (csarHelper.getServiceVfList() != null) {
149 processVfModules(resources, model, serviceNode);
152 if (hasSubCategoryTunnelXConnect(serviceMetadata) && hasAllottedResource(serviceMetadata)) {
153 model.addWidget(new TunnelXconnectWidget());
155 resources.add((Resource) model);
161 private void processVfModules(List<Resource> resources, Model model, NodeTemplate serviceNode) {
162 // Get the customisation UUID for each VF node and use it to get its Groups
163 String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode);
165 // Populate a Map of Group against NodeTemplates that are members of the Group
166 List<Group> serviceGroups = csarHelper.getVfModulesByVf(uuid);
168 // Process each VF Group
169 for (Group serviceGroup : serviceGroups) {
170 Model groupModel = Model.getModelFor(serviceGroup.getType());
171 if (groupModel instanceof VfModule) {
172 processVfModule(resources, model, serviceGroup, serviceNode, (VfModule) groupModel);
178 private void processVfModule(List<Resource> resources, Model model, Group groupDefinition, NodeTemplate serviceNode,
179 VfModule groupModel) {
180 // Populate group with metadata properties
181 groupModel.populateModelIdentificationInformation(groupDefinition.getMetadata().getAllProperties());
182 // Populate group with non-metadata properties
183 Map<String, Property> groupProperties = groupDefinition.getProperties();
184 Map<String, String> properties = populateStringProperties(groupProperties);
185 groupModel.populateModelIdentificationInformation(properties);
186 processVfModuleGroup(resources, model, groupDefinition, serviceNode, groupModel);
189 private void processVfModuleGroup(List<Resource> resources, Model model, Group groupDefinition,
190 NodeTemplate serviceNode, VfModule groupModel) {
191 // Get names of the members of the service group
192 List<NodeTemplate> members = csarHelper.getMembersOfVfModule(serviceNode, groupDefinition);
193 if (members != null && !members.isEmpty()) {
194 List<String> memberNames = members.stream().map(NodeTemplate::getName).collect(Collectors.toList());
195 groupModel.setMembers(memberNames);
196 for (NodeTemplate nodeTemplate : members) {
197 processNodeTemplate(groupModel, nodeTemplate);
201 model.addResource(groupModel); // Added group (VfModule) to the (VF) model
202 // Check if we have already encountered the same VfModule across all the artifacts
203 if (!resources.contains(groupModel)) {
204 resources.add(groupModel);
208 private static void processNodeTemplate(Model group, NodeTemplate nodeTemplate) {
210 // L3-network inside vf-module to be generated as Widget a special handling.
211 if (nodeTemplate.getType().contains("org.openecomp.resource.vl")) {
212 resourceNode = new L3NetworkWidget();
214 resourceNode = Model.getModelFor(nodeTemplate.getType());
216 if (resourceNode != null && !(resourceNode instanceof Resource)) {
217 Widget widget = (Widget) resourceNode;
218 widget.addKey(nodeTemplate.getName());
219 // Add the widget element encountered to the Group model
220 group.addWidget(widget);
225 * Process the service tosca
227 * @param service model of the service artifact
228 * @param idTypeStore ID->Type mapping
229 * @param nodeTemplates a list of service nodes
232 public void processServiceTosca(Service service, Map<String, String> idTypeStore,
233 List<NodeTemplate> nodeTemplates) {
234 log.debug("Inside Service Tosca ");
235 // Get the resource/widgets in the service according to the node-template types
236 for (NodeTemplate node : nodeTemplates) {
237 Model model = Model.getModelFor(correctNodeType(node));
239 model.populateModelIdentificationInformation(node.getMetaData().getAllProperties());
240 if (model instanceof Resource) {
241 // Keeping track of resource types and
242 // their uuid for identification during resource tosca processing
243 idTypeStore.put(model.getModelNameVersionId(), correctNodeType(node));
244 service.addResource((Resource) model);
246 service.addWidget((Widget) model);
252 private String correctNodeType(NodeTemplate nodeType) {
253 String correctedNodeType = nodeType.getType();
254 if (hasAllottedResource(nodeType.getMetaData().getAllProperties())) {
255 if (nodeType.getType().contains("org.openecomp.resource.vf.")) {
256 correctedNodeType = "org.openecomp.resource.vf.allottedResource";
258 if (nodeType.getType().contains("org.openecomp.resource.vfc.")) {
259 correctedNodeType = "org.openecomp.resource.vfc.AllottedResource";
262 return correctedNodeType;
265 private boolean hasAllottedResource(Map<String, String> metadata) {
266 return ALLOTTED_RESOURCE.equals(metadata.get(CATEGORY));
269 private boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) {
270 return TUNNEL_XCONNECT.equals(metadata.get(SUBCATEGORY));
274 * Create a Map of property name against String property value from the input Map
276 * @param inputMap The input Map
277 * @return Map of property name against String property value
279 private Map<String, String> populateStringProperties(Map<String, Property> inputMap) {
280 return inputMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
281 e -> e.getValue().getValue() == null ? "" : e.getValue().getValue().toString()));
284 private void processVfTosca(Map<String, String> idTypeStore, Model model, List<NodeTemplate> resourceNodes) {
285 boolean flag = false;
287 for (NodeTemplate resourceNodeTemplate : resourceNodes) {
288 Model resourceNode = Model.getModelFor(correctNodeType(resourceNodeTemplate));
289 if (resourceNode instanceof ProvidingService) {
291 Map<String, Property> nodeProperties = resourceNodeTemplate.getProperties();
292 if (nodeProperties.get("providing_service_uuid") == null
293 || nodeProperties.get("providing_service_invariant_uuid") == null) {
294 throw new IllegalArgumentException(
295 String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, model.getModelId()));
297 Map<String, String> properties = populateStringProperties(nodeProperties);
298 properties.put(VERSION, "1.0");
299 resourceNode.populateModelIdentificationInformation(properties);
300 model.addResource((Resource) resourceNode);
301 } else if (resourceNode instanceof Resource && !(resourceNode.getWidgetType().equals(Widget.Type.L3_NET))) {
302 idTypeStore.put(resourceNode.getModelNameVersionId(), correctNodeType(resourceNodeTemplate));
303 model.addResource((Resource) resourceNode);
307 if (model instanceof AllotedResource && !flag) {
308 throw new IllegalArgumentException(
309 String.format(GENERATOR_AAI_PROVIDING_SERVICE_MISSING, model.getModelId()));