Replace explicit type with dimond type
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / domain / ServiceDecomposition.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\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.mso.bpmn.core.domain;\r
22 \r
23 \r
24 import java.io.Serializable;\r
25 import java.util.ArrayList;\r
26 import java.util.Collections;\r
27 import java.util.Iterator;\r
28 import java.util.List;\r
29 \r
30 import com.fasterxml.jackson.annotation.JsonIgnore;\r
31 import com.fasterxml.jackson.annotation.JsonProperty;\r
32 import com.fasterxml.jackson.annotation.JsonRootName;\r
33 \r
34 import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil;\r
35 import org.openecomp.mso.bpmn.core.json.JsonDecomposingException;\r
36 \r
37 \r
38 /**\r
39  * Service Decomposition Structure\r
40  * This Java object contains service information:\r
41  * - Service model info\r
42  * - Service type and role\r
43  * - list of VNF resource's decompositon\r
44  * - list of network resource's decompositon\r
45  * - list of allotted resource's decompositon\r
46  */\r
47 @JsonRootName(value = "serviceResources")\r
48 //@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)\r
49 public class ServiceDecomposition extends JsonWrapper implements Serializable {\r
50 \r
51         private static final long serialVersionUID = 1L;\r
52 \r
53         @JsonProperty("modelInfo")\r
54         private ModelInfo modelInfo;\r
55         @JsonProperty("serviceType")\r
56         private String serviceType;\r
57         @JsonProperty("serviceRole")\r
58         private String serviceRole;\r
59         private ServiceInstance serviceInstance;\r
60         @JsonProperty("vnfResource")\r
61         private List <VnfResource>  vnfResources;\r
62         @JsonProperty("networkResource")\r
63         private List <NetworkResource>  networkResources;\r
64         @JsonProperty("allottedResource")\r
65         private List <AllottedResource>  allottedResources;\r
66 \r
67         public ServiceDecomposition () {\r
68         }\r
69 \r
70         //*****\r
71         //GET and SET section\r
72         /**\r
73          * Return just the service model portion of the Service Decomposition as a Java object.\r
74          * The service model object should support retrieval as JSON string that is formatted correctly for sending serviceModelInfo to Building Blocks.\r
75          * @return\r
76          */\r
77         public ModelInfo getModelInfo() {\r
78                 return modelInfo;\r
79         }\r
80         public void setModelInfo(ModelInfo modelInfo) {\r
81                 this.modelInfo = modelInfo;\r
82         }\r
83         public ServiceInstance getServiceInstance() {\r
84                 return serviceInstance;\r
85         }\r
86         public void setServiceInstance(ServiceInstance serviceInstance) {\r
87                 this.serviceInstance = serviceInstance;\r
88         }\r
89         public List<VnfResource> getServiceVnfs() {\r
90                 return vnfResources;\r
91         }\r
92         public void setServiceVnfs(List<VnfResource> vnfResources) {\r
93                 this.vnfResources = vnfResources;\r
94         }\r
95         public List<NetworkResource> getServiceNetworks() {\r
96                 return networkResources;\r
97         }\r
98         public void setServiceNetworks(List<NetworkResource> networkResources) {\r
99                 this.networkResources = networkResources;\r
100         }\r
101         public List<AllottedResource> getServiceAllottedResources() {\r
102                 return allottedResources;\r
103         }\r
104         public void setServiceAllottedResources(List<AllottedResource> allottedResources) {\r
105                 this.allottedResources = allottedResources;\r
106         }\r
107         public String getServiceType() {\r
108                 return serviceType;\r
109         }\r
110 \r
111         public void setServiceType(String serviceType) {\r
112                 this.serviceType = serviceType;\r
113         }\r
114 \r
115         public String getServiceRole() {\r
116                 return serviceRole;\r
117         }\r
118 \r
119         public void setServiceRole(String serviceRole) {\r
120                 this.serviceRole = serviceRole;\r
121         }\r
122         //*****\r
123 \r
124         //*****\r
125         //Access methods\r
126 \r
127 \r
128         /**\r
129          * This method returns one combined list of Resources of All Types\r
130          * @return\r
131          */\r
132         @JsonIgnore\r
133         public List<Resource> getServiceResources(){\r
134                 ArrayList serviceResources = new ArrayList();\r
135                 if(this.getServiceAllottedResources() != null){\r
136                         serviceResources.addAll(this.getServiceAllottedResources());\r
137                 }\r
138                 if(this.getServiceNetworks() != null){\r
139                         serviceResources.addAll(this.getServiceNetworks());\r
140                 }\r
141                 if(this.getServiceVnfs() != null){\r
142                         serviceResources.addAll(this.getServiceVnfs());\r
143                 }\r
144                 return serviceResources;\r
145         }\r
146 \r
147         /**\r
148          * This method returns String representation of one combined list of Resources of All Types\r
149          * @return\r
150          */\r
151         @JsonIgnore\r
152         public String getServiceResourcesJsonString(){\r
153                 StringBuilder serviceResourcesJsonStringBuffer = new StringBuilder();\r
154                 serviceResourcesJsonStringBuffer.append(listToJson((this.getServiceNetworks())));\r
155                 serviceResourcesJsonStringBuffer.append(listToJson((this.getServiceVnfs())));\r
156                 serviceResourcesJsonStringBuffer.append(listToJson((this.getServiceAllottedResources())));\r
157                 return serviceResourcesJsonStringBuffer.toString();\r
158         }\r
159 \r
160         /**\r
161          * Returns a JSON list of all Network Resource structures (i.e. the serialized NetworkDecomposition objects).\r
162          * @return\r
163          */\r
164         @JsonIgnore\r
165         public String getServiceNetworksJson(){\r
166                 return listToJson(this.getServiceNetworks());\r
167         }\r
168         /**\r
169          * Returns a JSON list of all VnfResource structures (i.e. the serialized VnfResource objects).\r
170          * @return\r
171          */\r
172         @JsonIgnore\r
173         public String getServiceVnfsJson(){\r
174                 return listToJson(this.getServiceVnfs());\r
175         }\r
176         /**\r
177          * Returns a JSON list of all Allotted Resource structures (i.e. the serialized AllottedResource objects).\r
178          * @return\r
179          */\r
180         @JsonIgnore\r
181         public String getServiceAllottedResourcesJson(){\r
182                 return listToJson(this.getServiceAllottedResources());\r
183         }\r
184 \r
185         //TODO - define Resource Object ID\r
186         @JsonIgnore\r
187         public String getVnfResource(String resourceObjectId) {\r
188 \r
189         for (Resource resource : getServiceResources()) {\r
190             //resource.getModelInfo().getModelInvariantId();\r
191 \r
192             if ("extracted information".equals(resourceObjectId)) {\r
193                 return resource.toJsonString();\r
194             }\r
195         }\r
196                 return "";\r
197         }\r
198 \r
199         //Methods to add Resource to the list\r
200         /**\r
201          * Add VNF resource to the list\r
202          * @param vnfResource\r
203          */\r
204         public void addVnfResource(Resource vnfResource) {\r
205                 if (vnfResources == null){\r
206                         vnfResources = new ArrayList<>();\r
207                 }\r
208                 this.vnfResources.add((VnfResource)vnfResource);\r
209         }\r
210         /**\r
211          * Add Network resource to the list\r
212          * @param networkResource\r
213          */\r
214         public void addNetworkResource(Resource networkResource) {\r
215                 if (networkResources == null){\r
216                         networkResources = new ArrayList<>();\r
217                 }\r
218                 this.networkResources.add((NetworkResource)networkResource);\r
219         }\r
220         /**\r
221          * Add Allotted resource to the list\r
222          * @param allottedResource\r
223          */\r
224         public void addAllottedResource(Resource allottedResource) {\r
225                 if (allottedResources == null){\r
226                         allottedResources = new ArrayList<>();\r
227                 }\r
228                 this.allottedResources.add((AllottedResource)allottedResource);\r
229         }\r
230 \r
231         /**\r
232          * Add resource to the list\r
233          * Given a ResourceDecomposition (subclass) object, add it to the Service Decomposition (in the appropriate category, e.g. as a VNF, Network, or Allotted Resource).\r
234          * As dependencies are not currently supported, add it to the end of any ordered lists.\r
235          * @param resource\r
236          */\r
237         public void addResource(Resource resource) {\r
238                 //create resource based upon type\r
239                 switch (resource.resourceType) {\r
240                 case VNF:\r
241                         this.addVnfResource(resource);\r
242                         break;\r
243                 case NETWORK:\r
244                         this.addNetworkResource(resource);\r
245                      break;\r
246                 case ALLOTTED_RESOURCE:\r
247                         this.addAllottedResource(resource);\r
248                     break;\r
249                 default:\r
250                      throw new IllegalArgumentException("Invalid resource type: " + resource.resourceType);\r
251                  }\r
252         }\r
253 \r
254         /**\r
255          * Add resource to the list\r
256          * @param jsonResource\r
257          */\r
258         public void addVnfResource(String jsonResource) throws JsonDecomposingException {\r
259                 VnfResource vnfResource = null;\r
260                 vnfResource = DecomposeJsonUtil.jsonToVnfResource(jsonResource);\r
261                 this.addVnfResource(vnfResource);\r
262         }\r
263         /**\r
264          * Add resource to the list\r
265          * @param jsonResource\r
266          */\r
267         public void addNetworkResource(String jsonResource) throws JsonDecomposingException {\r
268                 NetworkResource networkResource = null;\r
269                 networkResource = DecomposeJsonUtil.jsonToNetworkResource(jsonResource);\r
270                 this.addVnfResource(networkResource);\r
271         }\r
272         /**\r
273          * Add resource to the list\r
274          * @param jsonResource\r
275          */\r
276         public void addAllottedResource(String jsonResource) throws JsonDecomposingException {\r
277                 AllottedResource allottedResource = null;\r
278                 allottedResource = DecomposeJsonUtil.jsonToAllottedResource(jsonResource);\r
279                 this.addVnfResource(allottedResource);\r
280         }\r
281 \r
282         /**\r
283          * Given a ResourceDecomposition (subclass) object, locate it in the Service Decomposition by its unique ID, and replace the current version with the new one.\r
284          * This method should support concurrency control via an auto-incrementing field in the ResourceDecomposition class.\r
285          * @param newResource\r
286          * @return TRUE if replacement was a success\r
287          */\r
288         public boolean replaceResource(Resource newResource){\r
289                 boolean result = false;\r
290                 List serviceResources = getServiceResources();\r
291                 for (Resource resource : (Iterable<Resource>) serviceResources) {\r
292                         System.out.println("resource found");\r
293                         if (resource.resourceType == newResource.resourceType) {\r
294                                 System.out.println("resource type matches");\r
295                                 if (resource.getResourceId().equalsIgnoreCase(newResource.getResourceId())) {\r
296                                         System.out.println("resource id matches");\r
297                                         //returns TRUE if replacement is a success\r
298                                         result = Collections.replaceAll(serviceResources, resource, newResource);\r
299                                 }\r
300                         }\r
301                 }\r
302                 //set updated list into ServiceDecomposition\r
303                 this.setResourceList(serviceResources);\r
304                 return result;\r
305         }\r
306 \r
307         /**\r
308          * Given a ResourceDecomposition as a JSON string, locate it in the Service Decomposition by its unique ID,\r
309          *  and replace the current version with the new one.\r
310          * @param jsonString\r
311          * @return\r
312          */\r
313         public boolean replaceResource(String jsonString){\r
314                 //TODO: define unique ID for the Resource!\r
315                 return false;\r
316         }\r
317 \r
318         /**\r
319          *  Given a resource object ID, locate it in the Service Decomposition by its unique ID, and delete it.\r
320          * @param resource\r
321          * @return TRUE if delete was a success\r
322          */\r
323         public boolean deleteResource(Resource resource){\r
324                 List serviceResources = getServiceResources();\r
325                 for (Resource item : (Iterable<Resource>) serviceResources) {\r
326                         if (item.resourceType == resource.resourceType) {\r
327                                 if (item.getResourceId().equalsIgnoreCase(resource.getResourceId())) {\r
328                                         //returns TRUE if replacement is a success\r
329                                         return serviceResources.remove(resource);\r
330                                 }\r
331                         }\r
332                 }\r
333 \r
334                 return false;\r
335         }\r
336 \r
337         /**\r
338          * Generic method to set List of ResourceDecomposition objects\r
339          * @param resources\r
340          * @return\r
341          */\r
342         public boolean setResourceList(List<Resource> resources){\r
343                 //create resource based upon type\r
344                 switch (resources.get(0).resourceType) {\r
345                 case VNF:\r
346                         this.setServiceVnfs((List<VnfResource>)(List<?>)resources);\r
347                         break;\r
348                 case NETWORK:\r
349                         this.setServiceNetworks((List<NetworkResource>)(List<?>)resources);\r
350                     break;\r
351                 case ALLOTTED_RESOURCE:\r
352                         this.setServiceAllottedResources((List<AllottedResource>)(List<?>)resources);\r
353                     break;\r
354                 default:\r
355                      throw new IllegalArgumentException("Invalid resource type: " + resources.get(0).resourceType);\r
356                  }\r
357 \r
358                 return false;\r
359         }\r
360 \r
361         /**\r
362          *\r
363          * This method locates and returns a resource in a given\r
364          * Service Decomposition object by its unique resource id.\r
365          * Returns null if resource doesn't exist.\r
366          *\r
367          * @param resourceId - id of the resource\r
368          * @return resource\r
369          */\r
370         @JsonIgnore\r
371         public Resource getServiceResource(String resourceId){\r
372                 List<Resource> resources = getServiceResources();\r
373                 for (Resource resource : resources) {\r
374                         if (resource.getResourceId().equalsIgnoreCase(resourceId)) {\r
375                                 //match\r
376                                 return resource;\r
377                         }\r
378                 }\r
379                 return null;\r
380         }\r
381 }\r