Incorporate the ECOMP SDC Artefact Generator code
[aai/babel.git] / src / main / java / org / onap / aai / babel / parser / ArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.babel.parser;
22
23 import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGFILE_NOT_FOUND;
24 import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND;
25 import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING;
26 import static org.onap.aai.babel.xml.generator.data.GeneratorConstants.GENERATOR_AAI_PROVIDING_SERVICE_MISSING;
27
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.stream.Collectors;
36 import org.onap.aai.babel.logging.LogHelper;
37 import org.onap.aai.babel.xml.generator.data.GeneratorConstants;
38 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
39 import org.onap.aai.babel.xml.generator.model.AllotedResource;
40 import org.onap.aai.babel.xml.generator.model.L3NetworkWidget;
41 import org.onap.aai.babel.xml.generator.model.Model;
42 import org.onap.aai.babel.xml.generator.model.ProvidingService;
43 import org.onap.aai.babel.xml.generator.model.Resource;
44 import org.onap.aai.babel.xml.generator.model.Service;
45 import org.onap.aai.babel.xml.generator.model.TunnelXconnectWidget;
46 import org.onap.aai.babel.xml.generator.model.VfModule;
47 import org.onap.aai.babel.xml.generator.model.Widget;
48 import org.onap.aai.babel.xml.generator.types.ModelType;
49 import org.onap.aai.cl.api.Logger;
50 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
51 import org.onap.sdc.toscaparser.api.Group;
52 import org.onap.sdc.toscaparser.api.NodeTemplate;
53 import org.onap.sdc.toscaparser.api.Property;
54
55 public class ArtifactGeneratorToscaParser {
56
57     public static final String GENERATOR_AAI_ERROR_INVALID_ID =
58             "Invalid value for mandatory attribute <%s> in Artifact: <%s>";
59     private static final String ALLOTTED_RESOURCE = "Allotted Resource";
60     private static final String TUNNEL_XCONNECT = "Tunnel XConnect";
61     private static Logger log = LogHelper.INSTANCE;
62
63     private ISdcCsarHelper csarHelper;
64
65     /**
66      * Constructs using csarHelper
67      * 
68      * @param csarHelper The csar helper
69      */
70     public ArtifactGeneratorToscaParser(ISdcCsarHelper csarHelper) {
71         this.csarHelper = csarHelper;
72     }
73
74     /**
75      * Returns the artifact description
76      * 
77      * @param model the artifact model
78      * @return the artifact model's description
79      */
80     public static String getArtifactDescription(Model model) {
81         String artifactDesc = model.getModelDescription();
82         if (model.getModelType().equals(ModelType.SERVICE)) {
83             artifactDesc = "AAI Service Model";
84         } else if (model.getModelType().equals(ModelType.RESOURCE)) {
85             artifactDesc = "AAI Resource Model";
86         }
87         return artifactDesc;
88     }
89
90     /**
91      * Initialises the widget configuration.
92      * 
93      * @throws IOException
94      */
95     public static void initWidgetConfiguration() throws IOException {
96         log.debug("Getting Widget Configuration");
97         String configLocation = System.getProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
98         if (configLocation != null) {
99             File file = new File(configLocation);
100             if (file.exists()) {
101                 Properties properties = new Properties();
102                 properties.load(new FileInputStream(file));
103                 WidgetConfigurationUtil.setConfig(properties);
104             } else {
105                 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
106             }
107         } else {
108             throw new IllegalArgumentException(GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND);
109         }
110     }
111
112     /**
113      * Generates a Resource List using input Service Node Templates
114      * 
115      * @param serviceNodes input Service Node Templates
116      * @param idTypeStore ID->Type mapping
117      * @return the processed resource models
118      */
119     public List<Resource> processResourceToscas(List<NodeTemplate> serviceNodes, Map<String, String> idTypeStore) {
120         List<Resource> resources = new LinkedList<>();
121         for (NodeTemplate serviceNode : serviceNodes) {
122             List<NodeTemplate> resourceNodes = csarHelper.getNodeTemplateChildren(serviceNode);
123
124             String resourceUuId = serviceNode.getMetaData().getValue("UUID");
125             String mapValue = idTypeStore.get(resourceUuId);
126             if (mapValue != null) {
127                 Model model = Model.getModelFor(idTypeStore.get(serviceNode.getMetaData().getValue("UUID")));
128
129                 log.debug("Inside Resource artifact generation for resource");
130                 Map<String, String> serviceMetadata = serviceNode.getMetaData().getAllProperties();
131                 model.populateModelIdentificationInformation(serviceMetadata);
132
133                 // Found model from the type store so removing the same
134                 idTypeStore.remove(model.getModelNameVersionId());
135                 processVfTosca(idTypeStore, model, resourceNodes);
136
137                 // Process group information from tosca for vfModules
138                 if (csarHelper.getServiceVfList() != null) {
139                     processVfModules(resources, model, serviceNode);
140                 }
141
142                 if (hasSubCategoryTunnelXConnect(serviceMetadata) && hasAllottedResource(serviceMetadata)) {
143                     model.addWidget(new TunnelXconnectWidget());
144                 }
145                 resources.add((Resource) model);
146             }
147         }
148         return resources;
149     }
150
151     private void processVfModules(List<Resource> resources, Model model, NodeTemplate serviceNode) {
152         // Get the customisation UUID for each VF node and use it to get its Groups
153         String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode);
154
155         // Populate a Map of Group against NodeTemplates that are members of the Group
156         List<Group> serviceGroups = csarHelper.getVfModulesByVf(uuid);
157
158         // Process each VF Group
159         for (Group serviceGroup : serviceGroups) {
160             Model groupModel = Model.getModelFor(serviceGroup.getType());
161             if (groupModel instanceof VfModule) {
162                 processVfModule(resources, model, serviceGroup, serviceNode, (VfModule) groupModel);
163             }
164         }
165
166     }
167
168     private void processVfModule(List<Resource> resources, Model model, Group groupDefinition, NodeTemplate serviceNode,
169             VfModule groupModel) {
170         // Populate group with metadata properties
171         groupModel.populateModelIdentificationInformation(groupDefinition.getMetadata().getAllProperties());
172         // Populate group with non-metadata properties
173         Map<String, Property> groupProperties = groupDefinition.getProperties();
174         Map<String, String> properties = populateStringProperties(groupProperties);
175         groupModel.populateModelIdentificationInformation(properties);
176         processVfModuleGroup(resources, model, groupDefinition, serviceNode, groupModel);
177     }
178
179     private void processVfModuleGroup(List<Resource> resources, Model model, Group groupDefinition,
180             NodeTemplate serviceNode, VfModule groupModel) {
181         // Get names of the members of the service group
182         List<NodeTemplate> members = csarHelper.getMembersOfVfModule(serviceNode, groupDefinition);
183         if (members != null && !members.isEmpty()) {
184             List<String> memberNames = members.stream().map(NodeTemplate::getName).collect(Collectors.toList());
185             groupModel.setMembers(memberNames);
186             for (NodeTemplate nodeTemplate : members) {
187                 processNodeTemplate(groupModel, nodeTemplate);
188             }
189         }
190
191         model.addResource(groupModel); // Added group (VfModule) to the (VF) model
192         // Check if we have already encountered the same VfModule across all the artifacts
193         if (!resources.contains(groupModel)) {
194             resources.add(groupModel);
195         }
196     }
197
198     private static void processNodeTemplate(Model group, NodeTemplate nodeTemplate) {
199         Model resourceNode;
200         // L3-network inside vf-module to be generated as Widget a special handling.
201         if (nodeTemplate.getType().contains("org.openecomp.resource.vl")) {
202             resourceNode = new L3NetworkWidget();
203         } else {
204             resourceNode = Model.getModelFor(nodeTemplate.getType());
205         }
206         if (resourceNode != null && !(resourceNode instanceof Resource)) {
207             Widget widget = (Widget) resourceNode;
208             widget.addKey(nodeTemplate.getName());
209             // Add the widget element encountered to the Group model
210             group.addWidget(widget);
211         }
212     }
213
214     /**
215      * Process the service tosca
216      * 
217      * @param service model of the service artifact
218      * @param idTypeStore ID->Type mapping
219      * @param nodeTemplates a list of service nodes
220      * 
221      */
222     public void processServiceTosca(Service service, Map<String, String> idTypeStore,
223             List<NodeTemplate> nodeTemplates) {
224         log.debug("Inside Service Tosca ");
225         // Get the resource/widgets in the service according to the node-template types
226         for (NodeTemplate node : nodeTemplates) {
227             Model model = Model.getModelFor(correctNodeType(node));
228             if (model != null) {
229                 model.populateModelIdentificationInformation(node.getMetaData().getAllProperties());
230                 if (model instanceof Resource) {
231                     // Keeping track of resource types and
232                     // their uuid for identification during resource tosca processing
233                     idTypeStore.put(model.getModelNameVersionId(), correctNodeType(node));
234                     service.addResource((Resource) model);
235                 } else {
236                     service.addWidget((Widget) model);
237                 }
238             }
239         }
240     }
241
242     private String correctNodeType(NodeTemplate nodeType) {
243         String correctedNodeType = nodeType.getType();
244         if (hasAllottedResource(nodeType.getMetaData().getAllProperties())) {
245             if (nodeType.getType().contains("org.openecomp.resource.vf.")) {
246                 correctedNodeType = "org.openecomp.resource.vf.allottedResource";
247             }
248             if (nodeType.getType().contains("org.openecomp.resource.vfc.")) {
249                 correctedNodeType = "org.openecomp.resource.vfc.AllottedResource";
250             }
251         }
252         return correctedNodeType;
253     }
254
255     private boolean hasAllottedResource(Map<String, String> metadata) {
256         return ALLOTTED_RESOURCE.equals(metadata.get(GeneratorConstants.CATEGORY));
257     }
258
259     private boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) {
260         return TUNNEL_XCONNECT.equals(metadata.get(GeneratorConstants.SUBCATEGORY));
261     }
262
263     /**
264      * Create a Map of property name against String property value from the input Map
265      * 
266      * @param inputMap The input Map
267      * @return Map of property name against String property value
268      */
269     private Map<String, String> populateStringProperties(Map<String, Property> inputMap) {
270         return inputMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
271                 e -> e.getValue().getValue() == null ? "" : e.getValue().getValue().toString()));
272     }
273
274     private void processVfTosca(Map<String, String> idTypeStore, Model model, List<NodeTemplate> resourceNodes) {
275         boolean flag = false;
276
277         for (NodeTemplate resourceNodeTemplate : resourceNodes) {
278             Model resourceNode = Model.getModelFor(correctNodeType(resourceNodeTemplate));
279             if (resourceNode instanceof ProvidingService) {
280                 flag = true;
281                 Map<String, Property> nodeProperties = resourceNodeTemplate.getProperties();
282                 if (nodeProperties.get("providing_service_uuid") == null
283                         || nodeProperties.get("providing_service_invariant_uuid") == null) {
284                     throw new IllegalArgumentException(
285                             String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, model.getModelId()));
286                 }
287                 Map<String, String> properties = populateStringProperties(nodeProperties);
288                 properties.put(GeneratorConstants.VERSION, "1.0");
289                 resourceNode.populateModelIdentificationInformation(properties);
290                 model.addResource((Resource) resourceNode);
291             } else if (resourceNode instanceof Resource && !(resourceNode.getWidgetType().equals(Widget.Type.L3_NET))) {
292                 idTypeStore.put(resourceNode.getModelNameVersionId(), correctNodeType(resourceNodeTemplate));
293                 model.addResource((Resource) resourceNode);
294             }
295         }
296
297         if (model instanceof AllotedResource && !flag) {
298             throw new IllegalArgumentException(
299                     String.format(GENERATOR_AAI_PROVIDING_SERVICE_MISSING, model.getModelId()));
300         }
301     }
302 }