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