Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / model / NewServiceModel.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.openecomp.vid.model;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import org.openecomp.vid.asdc.beans.tosca.Group;
30 import org.openecomp.vid.asdc.beans.tosca.ToscaModel;
31 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
32 import org.openecomp.vid.properties.VidProperties;
33 /**
34  * The Class ServiceModel.
35  */
36 public class NewServiceModel {
37
38         /** The Constant LOG. */
39         private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(NewServiceModel.class);
40         
41         /** The Constant dateFormat. */
42         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
43         /** The service. */
44         private Service service;
45         
46         /** The vnfs. */
47         private Map<String, VNF> vnfs;
48         
49         /** The networks. */
50         private Map<String, Network> networks;
51
52         /** 
53          * The vf modules. The VNF also has vfmodules but the vfmodules at the service level may have additional info
54          * that is not present in the VNF, like the vf module customization String 
55          */
56         private Map<String, VfModule> vfModules;
57         /**
58          * The volume groups. The VNF also has volume groups but the volume groups will be populated at the service level
59          * for newer models
60          */
61         private Map<String, VolumeGroup> volumeGroups;
62         /**
63          * Instantiates a new service model.
64          */
65         public NewServiceModel() {}
66         
67         /**
68          * Gets the service.
69          *
70          * @return the service
71          */
72         public Service getService() {
73                 return service;
74         }
75
76         /**
77          * Gets the vnfs.
78          *
79          * @return the vnfs
80          */
81         public Map<String, VNF> getVnfs() {
82                 return vnfs;
83         }
84
85         /**
86          * Gets the networks.
87          *
88          * @return the networks
89          */
90         public Map<String, Network> getNetworks() {
91                 return networks;
92         }
93
94         /**
95          * Sets the service.
96          *
97          * @param service the new service
98          */
99         public void setService(Service service) {
100                 this.service = service;
101         }
102
103         /**
104          * Sets the vnfs.
105          *
106          * @param vnfs the vnfs
107          */
108         public void setVnfs(Map<String, VNF> vnfs) {
109                 this.vnfs = vnfs;
110         }
111
112         /**
113          * Sets the networks.
114          *
115          * @param networks the networks
116          */
117         public void setNetworks(Map<String, Network> networks) {
118                 this.networks = networks;
119         }
120         /**
121          * Gets the vf modules.
122          *
123          * @return the vf modules
124          */
125         public Map<String, VfModule> getVfModules() {
126                 return vfModules;
127         }
128         /**
129          * Gets the volume groups.
130          *
131          * @return the volume groups
132          */
133         public Map<String, VolumeGroup> getVolumeGroups() {
134                 return volumeGroups;
135         }
136         /**
137          * Sets the vf modules.
138          *
139          * @param vfModules the vf modules
140          */
141         public void setVfModules(Map<String, VfModule> vfModules) {
142                 this.vfModules = vfModules;
143         }
144         /**
145          * Sets the volume groups.
146          *
147          * @param volumeGroups the volume groups
148          */
149         public void setVolumeGroups(Map<String, VolumeGroup> volumeGroups) {
150                 this.volumeGroups = volumeGroups;
151         }
152         /**
153          * Extract service.
154          *
155          * @param serviceToscaModel the service tosca model
156          * @param asdcServiceMetadata the asdc service metadata
157          * @return the service
158          */
159         public static Service extractService(ToscaModel serviceToscaModel, org.openecomp.vid.asdc.beans.Service asdcServiceMetadata) {
160                 
161                 final Service service = new Service();
162                 
163                 service.setCategory(serviceToscaModel.getMetadata().getCategory());
164                 service.setInvariantUuid(serviceToscaModel.getMetadata().getInvariantUUID());
165                 service.setName(serviceToscaModel.getMetadata().getName());
166                 service.setUuid(serviceToscaModel.getMetadata().getUUID());
167                 service.setDescription(serviceToscaModel.getMetadata().getDescription());
168                 service.setServiceEcompNaming(serviceToscaModel.getMetadata().getServiceEcompNaming());
169                 service.setInputs(serviceToscaModel.gettopology_template().getInputs());
170                 //FIXME: SDC is not sending the Version with the Tosca Model for 1610 - they should send it in 1702
171                 //THIS IS A TEMPORARY FIX, AT SOME POINT UNCOMMENT ME
172                 //service.setVersion(serviceToscaModel.getMetadata().getVersion());
173                 service.setVersion(asdcServiceMetadata.getVersion());
174
175                 return service;
176         }
177         public static void extractGroups (ToscaModel serviceToscaModel,NewServiceModel serviceModel) {
178                 // Get the groups. The groups may duplicate the groups that are in the VNF model and have
179                 // additional data like the VF module customization String>
180                 
181                 final Map<String, VfModule> vfModules = new HashMap<String, VfModule> ();
182                 final Map<String, VolumeGroup> volumeGroups = new HashMap<String, VolumeGroup> ();
183                 
184                 String asdcModelNamespace = VidProperties.getAsdcModelNamespace();
185         String vfModuleTag = asdcModelNamespace + ModelConstants.VF_MODULE;
186         
187                 for (Entry<String, Group> component : serviceToscaModel.gettopology_template().getGroups().entrySet()) {
188                         final Group group = component.getValue();
189                         final String type = group.getType();
190                         final String customizationName = component.getKey();
191                         
192                         if (type.startsWith(vfModuleTag)) {
193                                 VfModule vfMod = VfModule.extractVfModule(customizationName, group);
194                                 vfModules.put(customizationName, vfMod);
195                                 if ( vfMod.isVolumeGroupAllowed() ) {
196                                         //volume groups have the same customization name as the vf module
197                                         volumeGroups.put(customizationName, VolumeGroup.extractVolumeGroup(customizationName,group));
198                                 }
199                         }
200                 }
201                 // add this point vfModules and volume groups are disconnected from VNF
202                 serviceModel.setVfModules (vfModules);
203                 serviceModel.setVolumeGroups (volumeGroups);
204                 
205         }
206         /**
207          * Populate the vf modules and volume groups that we may have under the service level under each VNF.
208          */
209 //      public void associateGroups() {
210 //              String methodName = "associateGroups()";
211 //        LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");
212 //              // go through the vnfs, get the vnf normalized name and look for a vf module with a customization name that starts
213 //              // with vnf + ".."
214 //              String vnfCustomizationName = null;
215 //              String normalizedVnfCustomizationName = null;
216 //              String vfModuleCustomizationName = null;
217 //              NewVNF tmpVnf = null;
218 //
219 //              if ( ( getVnfs() != null ) && (!(getVnfs().isEmpty())) ) {
220 //                      for (Entry<String, NewVNF> vnfComponent : getVnfs().entrySet()) {
221 //                              vnfCustomizationName = vnfComponent.getValue().getModelCustomizationName();
222 //                              normalizedVnfCustomizationName = VNF.normalizeName(vnfCustomizationName);
223 //
224 //                              LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName +
225 //                                              " VNF customizationName=" + vnfCustomizationName + "normalized customization name=" + normalizedVnfCustomizationName);
226 //
227 //                              // now check to see if there is a vf module with customization name that starts with normalizedVnfCustomizationName
228 //
229 //                              if (( getVfModules() != null ) && (!(getVfModules().isEmpty()))) {
230 //                                      for (Entry<String, VfModule> vfModuleComponent : getVfModules().entrySet()) {
231 //                                              vfModuleCustomizationName = vfModuleComponent.getValue().getModelCustomizationName();
232 //
233 //                                              LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName +
234 //                                                              " VF Module customizationName=" + vfModuleCustomizationName );
235 //                                              if ( vfModuleCustomizationName.startsWith(normalizedVnfCustomizationName + ".." )) {
236 //
237 //                                                      // this vf module belongs to the VNF
238 //                                                      tmpVnf = vnfComponent.getValue();
239 //                                                      (tmpVnf.getVfModules()).put(vfModuleComponent.getKey(), vfModuleComponent.getValue());
240 //
241 //                                                      LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName +
242 //                                                                      " Associated VF Module customizationName=" + vfModuleComponent.getKey() + " with VNF customization name=" + vnfCustomizationName);
243 //
244 //                                                      // 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
245 //                                                      if ( vfModuleComponent.getValue().isVolumeGroupAllowed() ) {
246 //                                                              if (( getVolumeGroups() != null ) && (!(getVolumeGroups().isEmpty()))) {
247 //                                                                      if (getVolumeGroups().containsKey((vfModuleCustomizationName))) {
248 //                                                                              (vnfComponent.getValue().getVolumeGroups()).put(vfModuleCustomizationName, (getVolumeGroups()).get(vfModuleCustomizationName));
249 //                                                                      }
250 //                                                              }
251 //                                                      }
252 //                                              }
253 //                                      }
254 //                              }
255 //                      }
256 //              }
257                 
258 //      }
259 }