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