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