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