53eb18c64899115697d383ec5390a2a926cd603c
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / ServiceModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.model;
22
23 import org.apache.commons.collections.MapUtils;
24 import org.onap.vid.asdc.beans.tosca.Group;
25 import org.onap.vid.asdc.beans.tosca.ToscaModel;
26 import org.onap.vid.properties.VidProperties;
27 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
28
29 import java.text.DateFormat;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 /**
36  * The Class ServiceModel.
37  */
38 @SuppressWarnings("ALL")
39 public class ServiceModel {
40
41         /** The Constant LOG. */
42         private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ServiceModel.class);
43         
44         /** The Constant dateFormat. */
45         static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
46         /** The service. */
47         private Service service;
48         
49         /** The vnfs. */
50         private Map<String, VNF> vnfs;
51         
52         /** The networks. */
53         private Map<String, Network> networks;
54
55         private Map<String, CR> collectionResource;
56
57
58         /** Port Mirroring Configuration node templates */
59         private Map<String, PortMirroringConfig> configurations;
60
61         /** Service Proxy Nodes */
62         private Map<String, ServiceProxy> serviceProxies;
63
64         /**
65          * The vf modules. The VNF also has vfmodules but the vfmodules at the service level may have additional info
66          * that is not present in the VNF, like the vf module customization String 
67          */
68         private Map<String, VfModule> vfModules;
69         /** 
70          * The volume groups. The VNF also has volume groups but the volume groups will be populated at the service level 
71          * for newer models 
72          */
73         private Map<String, VolumeGroup> volumeGroups;
74
75         /** The pnfs. */
76         private Map<String, Node> pnfs;
77         /**
78          * Instantiates a new service model.
79          */
80         public ServiceModel() {}
81         
82         /**
83          * Gets the service.
84          *
85          * @return the service
86          */
87         public Service getService() {
88                 return service;
89         }
90
91         /**
92          * Gets the vnfs.
93          *
94          * @return the vnfs
95          */
96         public Map<String, VNF> getVnfs() {
97                 return vnfs;
98         }
99
100         /**
101          * Gets the networks.
102          *
103          * @return the networks
104          */
105         public Map<String, Network> getNetworks() {
106                 return networks;
107         }
108
109
110         /**
111          * Gets the pnfs.
112          *
113          * @return the pnfs
114          */
115         public Map<String, Node> getPnfs() {
116                 return pnfs;
117         }
118
119
120         /**
121          * Gets the Configuration Node Templates
122          *
123          * @return the configuration type node templates
124          */
125         public Map<String, PortMirroringConfig> getConfigurations() {
126                 return configurations;
127         }
128
129         /**
130          * Gets the Service Proxy Node Templates
131          *
132          * @return the Service Proxy type node templates
133          */
134         public Map<String, ServiceProxy> getServiceProxies() {
135                 return serviceProxies;
136         }
137
138         /**
139          * Sets the service.
140          *
141          * @param service the new service
142          */
143         public void setService(Service service) {
144                 this.service = service;
145         }
146
147         /**
148          * Sets the vnfs.
149          *
150          * @param vnfs the vnfs
151          */
152         public void setVnfs(Map<String, VNF> vnfs) {
153                 this.vnfs = vnfs;
154         }
155
156         /**
157          * Sets the networks.
158          *
159          * @param networks the networks
160          */
161         public void setNetworks(Map<String, Network> networks) {
162                 this.networks = networks;
163         }
164
165         /**
166          * Sets the configuraion node templates.
167          *
168          * @param configurations
169          */
170         public void setConfigurations(Map<String, PortMirroringConfig> configurations) {
171                 this.configurations = configurations;
172         }
173
174         /**
175          * Sets the service proxy node templates.
176          *
177          * @param serviceProxies
178          */
179         public void setServiceProxies(Map<String, ServiceProxy> serviceProxies) {
180                 this.serviceProxies = serviceProxies;
181         }
182
183         /**
184          * Gets the vf modules.
185          *
186          * @return the vf modules
187          */
188         public Map<String, VfModule> getVfModules() {
189                 return vfModules;
190         }
191
192
193         /**
194          * Gets the volume groups.
195          *
196          * @return the volume groups
197          */
198         public Map<String, VolumeGroup> getVolumeGroups() {
199                 return volumeGroups;
200         }
201         /**
202          * Sets the vf modules.
203          *
204          * @param vfModules the vf modules
205          */
206         public void setVfModules(Map<String, VfModule> vfModules) {
207                 this.vfModules = vfModules;
208         }
209         /**
210          * Sets the volume groups.
211          *
212          * @param volumeGroups the volume groups
213          */
214         public void setVolumeGroups(Map<String, VolumeGroup> volumeGroups) {
215                 this.volumeGroups = volumeGroups;
216         }
217         /**
218          * Sets the pnfs.
219          *
220          * @param pnfs the pnfs
221          */
222         public void setPnfs(Map<String,Node> pnfs) {this.pnfs = pnfs;}
223
224         public Map<String, CR> getCollectionResource() {
225                 return collectionResource;
226         }
227
228         public void setCollectionResource(Map<String, CR> collectionResource) {
229                 this.collectionResource = collectionResource;
230         }
231
232         /**
233          * Extract service.
234          *
235          * @param serviceToscaModel the service tosca model
236          * @param asdcServiceMetadata the asdc service metadata
237          * @return the service
238          */
239         public static Service extractService(ToscaModel serviceToscaModel, org.onap.vid.asdc.beans.Service asdcServiceMetadata) {
240                 
241                 final Service service = new Service();
242                 
243                 service.setCategory(serviceToscaModel.getMetadata().getCategory());
244                 service.setInvariantUuid(serviceToscaModel.getMetadata().getInvariantUUID());
245                 service.setName(serviceToscaModel.getMetadata().getName());
246                 service.setUuid(serviceToscaModel.getMetadata().getUUID());
247                 service.setDescription(serviceToscaModel.getMetadata().getDescription());
248                 service.setServiceEcompNaming(serviceToscaModel.getMetadata().getServiceEcompNaming());
249                 service.setInputs(serviceToscaModel.gettopology_template().getInputs());
250                 //FIXME: SDC is not sending the Version with the Tosca Model for 1610 - they should send it in 1702
251                 //THIS IS A TEMPORARY FIX, AT SOME POINT UNCOMMENT ME
252                 //service.setVersion(serviceToscaModel.getMetadata().getVersion());
253                 service.setVersion(asdcServiceMetadata.getVersion());
254
255                 return service;
256         }
257         public static void extractGroups (ToscaModel serviceToscaModel,ServiceModel serviceModel) {
258                 // Get the groups. The groups may duplicate the groups that are in the VNF model and have
259                 // additional data like the VF module customization String>
260                 
261                 final Map<String, VfModule> vfModules = new HashMap<> ();
262                 final Map<String, VolumeGroup> volumeGroups = new HashMap<> ();
263                 
264                 String asdcModelNamespace = VidProperties.getAsdcModelNamespace();
265         String vfModuleTag = asdcModelNamespace + ModelConstants.VF_MODULE;
266         
267                 for (Entry<String, Group> component : serviceToscaModel.gettopology_template().getGroups().entrySet()) {
268                         final Group group = component.getValue();
269                         final String type = group.getType();
270                         final String customizationName = component.getKey();
271                         
272                         if (type.startsWith(vfModuleTag)) {
273                                 VfModule vfMod = VfModule.extractVfModule(customizationName, group);
274                                 vfModules.put(customizationName, vfMod);
275                                 if ( vfMod.isVolumeGroupAllowed() ) {
276                                         //volume groups have the same customization name as the vf module
277                                         volumeGroups.put(customizationName, VolumeGroup.extractVolumeGroup(customizationName,group));
278                                 }
279                         }
280                 }
281                 // add this point vfModules and volume groups are disconnected from VNF
282                 serviceModel.setVfModules (vfModules);
283                 serviceModel.setVolumeGroups (volumeGroups);
284                 
285         }
286         /**
287          * Populate the vf modules and volume groups that we may have under the service level under each VNF.
288          */
289         public void associateGroups() {
290                 String methodName = "associateGroups()";
291         LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");
292                 // go through the vnfs, get the vnf normalized name and look for a vf module with a customization name that starts
293                 // with vnf + ".."
294                 String vnfCustomizationName = null;
295                 String normalizedVnfCustomizationName = null;
296                 String vfModuleCustomizationName = null;
297
298                 if (!MapUtils.isEmpty(getVnfs())) {
299                         for (Entry<String, VNF> vnfComponent : getVnfs().entrySet()) {
300                                 vnfCustomizationName = vnfComponent.getValue().getModelCustomizationName();
301                                 normalizedVnfCustomizationName = VNF.normalizeName(vnfCustomizationName);
302
303                                 LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + 
304                                                 " VNF customizationName=" + vnfCustomizationName + "normalized customization name=" + normalizedVnfCustomizationName);
305                                 
306                                 // now check to see if there is a vf module with customization name that starts with normalizedVnfCustomizationName
307
308                                 if (!MapUtils.isEmpty(getVolumeGroups())) {
309                                         for (Entry<String, VfModule> vfModuleComponent : getVfModules().entrySet()) {
310                                                 vfModuleCustomizationName = vfModuleComponent.getValue().getModelCustomizationName();
311                                                 
312                                                 LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + 
313                                                                 " VF Module customizationName=" + vfModuleCustomizationName );
314                                                 if ( vfModuleCustomizationName.startsWith(normalizedVnfCustomizationName + ".." )) {
315                                                         handleCustomizationName(methodName, vnfCustomizationName, vfModuleCustomizationName, vnfComponent, vfModuleComponent);
316                                                 }
317                                         }
318                                 }
319                         }
320                 }
321                 
322         }
323
324
325
326         private void handleCustomizationName(String methodName, String vnfCustomizationName, String vfModuleCustomizationName, Entry<String, VNF> vnfComponent, Entry<String, VfModule> vfModuleComponent) {
327                 VNF tmpVnf;// this vf module belongs to the VNF
328                 tmpVnf = vnfComponent.getValue();
329                 (tmpVnf.getVfModules()).put(vfModuleComponent.getKey(), vfModuleComponent.getValue());
330
331                 LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName +
332                 " Associated VF Module customizationName=" + vfModuleComponent.getKey() + " with VNF customization name=" + vnfCustomizationName);
333
334                 // now find if this vf module has volume groups, if so, find the volume group with the same customization name and put it under the VNF
335                 if ( vfModuleComponent.getValue().isVolumeGroupAllowed() ) {
336             if (isVolumeGroupsContainsVfModuleCustomName(vfModuleCustomizationName)) {
337                     (vnfComponent.getValue().getVolumeGroups()).put(vfModuleCustomizationName, (getVolumeGroups()).get(vfModuleCustomizationName));
338             }
339         }
340         }
341
342         private boolean isVolumeGroupsContainsVfModuleCustomName(String vfModuleCustomizationName) {
343                 return (!MapUtils.isEmpty(getVolumeGroups())) && (getVolumeGroups().containsKey((vfModuleCustomizationName)));
344         }
345
346
347 }