5e7a4796c4ac4835729b55ae54bc8ca420af7b37
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / onap / so / bpmn / core / domain / ServiceDecomposition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.so.bpmn.core.domain;
22
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import com.fasterxml.jackson.annotation.JsonIgnore;
31 import com.fasterxml.jackson.annotation.JsonProperty;
32 import com.fasterxml.jackson.annotation.JsonRootName;
33 import org.json.JSONObject;
34 import org.onap.so.bpmn.core.json.DecomposeJsonUtil;
35 import org.onap.so.bpmn.core.json.JsonDecomposingException;
36
37
38
39
40 /**
41  * Service Decomposition Structure
42  * This Java object contains service information:
43  * - Service model info
44  * - Service type and role
45  * - list of VNF resource's decompositon
46  * - list of network resource's decompositon
47  * - list of allotted resource's decompositon
48  */
49 @JsonRootName(value = "serviceResources")
50 //@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)
51 public class ServiceDecomposition extends JsonWrapper implements Serializable {
52
53         private static final long serialVersionUID = 1L;
54
55         @JsonProperty("modelInfo")
56         private ModelInfo modelInfo;
57         @JsonProperty("serviceType")
58         private String serviceType;
59         @JsonProperty("serviceRole")
60         private String serviceRole;
61         private ServiceInstance serviceInstance;
62         private Request request;
63         private Customer customer;
64         private String callbackURN;
65         private String sdncVersion;
66         @JsonProperty("project")
67         private Project project;
68         @JsonProperty("owningEntity")
69         private OwningEntity owningEntity;
70         @JsonProperty("serviceVnfs")
71         private List <VnfResource> vnfResources;
72         @JsonProperty("serviceNetworks")
73         private List <NetworkResource> networkResources;
74         @JsonProperty("serviceAllottedResources")
75         private List <AllottedResource> allottedResources;
76         @JsonProperty("configResource")
77         private List <ConfigResource> configResources;
78
79         public ServiceDecomposition () {
80                 super();
81         }
82
83         public ServiceDecomposition (String catalogRestOutput) throws JsonDecomposingException {
84                 ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.jsonToServiceDecomposition(catalogRestOutput);
85                 this.modelInfo = serviceDecomposition.getModelInfo();
86                 this.vnfResources = serviceDecomposition.getVnfResources();
87                 this.allottedResources = serviceDecomposition.getAllottedResources();
88                 this.networkResources = serviceDecomposition.getNetworkResources();
89                 this.serviceRole = serviceDecomposition.getServiceRole();
90                 this.serviceType = serviceDecomposition.getServiceType();
91                 this.configResources = serviceDecomposition.getConfigResources();
92         }
93
94         /**
95          * Constructor taking Catalog DB Adapter REST output (serviceResources model) + service Instance ID
96          *
97          * @param catalogRestOutput
98          * @param serviceInstanceId
99          */
100         public ServiceDecomposition (String catalogRestOutput, String serviceInstanceId) throws JsonDecomposingException {
101                 ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.jsonToServiceDecomposition(catalogRestOutput);
102                 this.modelInfo = serviceDecomposition.getModelInfo();
103                 this.vnfResources = serviceDecomposition.getVnfResources();
104                 this.allottedResources = serviceDecomposition.getAllottedResources();
105                 this.configResources = serviceDecomposition.getConfigResources();
106                 this.networkResources = serviceDecomposition.getNetworkResources();
107
108                 this.serviceRole = serviceDecomposition.getServiceRole();
109                 this.serviceType = serviceDecomposition.getServiceType();
110
111                 this.serviceInstance = new ServiceInstance();
112                 this.serviceInstance.setInstanceId(serviceInstanceId);
113
114                 this.project = serviceDecomposition.getProject();
115                 this.owningEntity = serviceDecomposition.getOwningEntity();
116         }
117
118         /**
119          * Constructor taking a Service Decomposition JSON serialization
120          * @param catalogRestOutput
121          * @param serviceInstanceId
122          */
123         public ServiceDecomposition (JSONObject jsonServiceDecomposition, String serviceInstanceId) {
124                 //TODO provide constructor implementation
125
126         }
127
128         //*****
129         //GET and SET section
130         /**
131          * Return just the service model portion of the Service Decomposition as a Java object.
132          * The service model object should support retrieval as JSON string that is formatted correctly for sending serviceModelInfo to Building Blocks.
133          * @return
134          */
135         public ModelInfo getModelInfo() {
136                 return modelInfo;
137         }
138         public void setModelInfo(ModelInfo modelInfo) {
139                 this.modelInfo = modelInfo;
140         }
141         public ServiceInstance getServiceInstance() {
142                 return serviceInstance;
143         }
144         public void setServiceInstance(ServiceInstance serviceInstance) {
145                 this.serviceInstance = serviceInstance;
146         }
147         public Project getProject() {
148                 return project;
149         }
150         public OwningEntity getOwningEntity() {
151                 return owningEntity;
152         }
153         public void setProject(Project project) {
154                 this.project = project;
155         }
156         public void setOwningEntity(OwningEntity owningEntity) {
157                 this.owningEntity = owningEntity;
158         }
159         public List<VnfResource> getVnfResources() {
160                 return vnfResources;
161         }
162         public void setVnfResources(List<VnfResource> vnfResources) {
163                 this.vnfResources = vnfResources;
164         }
165         public void setConfigResources(List<ConfigResource> configResources) {
166                 this.configResources = configResources;
167         }
168         public List<ConfigResource> getConfigResources() {
169                 return configResources;
170         }
171         public void setNetworkResources(List<NetworkResource> networkResources) {
172                 this.networkResources = networkResources;
173         }
174         public List<NetworkResource> getNetworkResources() {
175                 return networkResources;
176         }
177         public List<AllottedResource> getAllottedResources() {
178                 return allottedResources;
179         }
180         public void setAllottedResources(List<AllottedResource> allottedResources) {
181                 this.allottedResources = allottedResources;
182         }
183         public String getServiceType() {
184                 return serviceType;
185         }
186
187         public void setServiceType(String serviceType) {
188                 this.serviceType = serviceType;
189         }
190
191         public String getServiceRole() {
192                 return serviceRole;
193         }
194
195         public void setServiceRole(String serviceRole) {
196                 this.serviceRole = serviceRole;
197         }
198         public Request getRequest() {
199                 return request;
200         }
201
202         public void setRequest(Request request) {
203                 this.request = request;
204         }
205         public Customer getCustomer() {
206                 return customer;
207         }
208         public void setCustomer(Customer customer) {
209                 this.customer = customer;
210         }
211         public String getCallbackURN() {
212                 return callbackURN;
213         }
214
215         public void setCallbackURN(String callbackURN) {
216                 this.callbackURN = callbackURN;
217         }
218         public String getSdncVersion() {
219                 return sdncVersion;
220         }
221
222         public void setSdncVersion(String sdncVersion) {
223                 this.sdncVersion = sdncVersion;
224         }
225
226         //*****
227
228         //*****
229         //Access methods
230
231
232         /**
233          * This method returns one combined list of Resources of All Types
234          * @return
235          */
236         @JsonIgnore
237         public List<Resource> getServiceResources(){
238                 ArrayList serviceResources = new ArrayList();
239                 if(this.getAllottedResources() != null){
240                         serviceResources.addAll(this.getAllottedResources());
241                 }
242                 if(this.getNetworkResources() != null){
243                         serviceResources.addAll(this.getNetworkResources());
244                 }
245                 if(this.getVnfResources() != null){
246                         serviceResources.addAll(this.getVnfResources());
247                 }
248                 if(this.getConfigResources() != null){
249                         serviceResources.addAll(this.getConfigResources());
250                 }
251                 return serviceResources;
252         }
253
254         /**
255          * This method returns String representation of one combined list of Resources of All Types
256          */
257         @JsonIgnore
258         public String getServiceResourcesJsonString() {
259                 return getServiceNetworksJson() +
260                                 getServiceVnfsJson() +
261                                 getServiceAllottedResourcesJson() +
262                                 getServiceConfigResourcesJson();
263         }
264
265         /**
266          * Returns a JSON list of all Network Resource structures (i.e. the serialized NetworkDecomposition objects).
267          * @return
268          */
269         @JsonIgnore
270         public String getServiceNetworksJson(){
271                 return listToJson(this.getNetworkResources());
272         }
273         /**
274          * Returns a JSON list of all VnfResource structures (i.e. the serialized VnfResource objects).
275          * @return
276          */
277         @JsonIgnore
278         public String getServiceVnfsJson(){
279                 return listToJson(this.getVnfResources());
280         }
281         /**
282          * Returns a JSON list of all Allotted Resource structures (i.e. the serialized AllottedResource objects).
283          * @return
284          */
285         @JsonIgnore
286         public String getServiceAllottedResourcesJson(){
287                 return listToJson(this.getAllottedResources());
288         }
289         /**
290          * Returns a JSON list of all Config Resource structures (i.e. the serialized ConfigResource objects).
291          * @return
292          */
293         @JsonIgnore
294         public String getServiceConfigResourcesJson(){
295                 return listToJson(this.getConfigResources());
296         }
297
298         //TODO - define Resource Object ID
299         @JsonIgnore
300         public String getVnfResource(String resourceObjectId) {
301
302         for (Resource resource : getServiceResources()) {
303             //resource.getModelInfo().getModelInvariantId();
304
305             if ("extracted information".equals(resourceObjectId)) {
306                 return resource.toJsonString();
307             }
308         }
309                 return "";
310         }
311
312         //Methods to add Resource to the list
313         /**
314          * Add VNF resource to the list
315          * @param vnfResource
316          */
317         public void addVnfResource(Resource vnfResource) {
318                 if (vnfResources == null){
319                         vnfResources = new ArrayList<>();
320                 }
321                 this.vnfResources.add((VnfResource)vnfResource);
322         }
323         /**
324          * Add Network resource to the list
325          * @param networkResource
326          */
327         public void addNetworkResource(Resource networkResource) {
328                 if (networkResources == null){
329                         networkResources = new ArrayList<>();
330                 }
331                 this.networkResources.add((NetworkResource)networkResource);
332         }
333         /**
334          * Add Allotted resource to the list
335          * @param allottedResource
336          */
337         public void addAllottedResource(Resource allottedResource) {
338                 if (allottedResources == null){
339                         allottedResources = new ArrayList<>();
340                 }
341                 this.allottedResources.add((AllottedResource)allottedResource);
342         }
343         /**
344          * Add Config resource to the list
345          * @param allottedResource
346          */
347         public void addConfigResource(Resource configResource) {
348                 if (configResources == null){
349                         configResources = new ArrayList<>();
350                 }
351                 this.configResources.add((ConfigResource)configResource);
352         }
353         /**
354          * Add resource to the list
355          * Given a ResourceDecomposition (subclass) object, add it to the Service Decomposition (in the appropriate category, e.g. as a VNF, Network, or Allotted Resource).
356          * As dependencies are not currently supported, add it to the end of any ordered lists.
357          * @param resource
358          */
359         public void addResource(Resource resource) {
360                 //create resource based upon type
361                 switch (resource.resourceType) {
362                 case VNF:
363                         this.addVnfResource(resource);
364                         break;
365                 case NETWORK:
366                         this.addNetworkResource(resource);
367                      break;
368                 case ALLOTTED_RESOURCE:
369                         this.addAllottedResource(resource);
370                     break;
371                 case CONFIGURATION:
372                         this.addConfigResource(resource);
373                     break;
374                 default:
375                      throw new IllegalArgumentException("Invalid resource type: " + resource.resourceType);
376                  }
377         }
378
379         /**
380          * Add resource to the list
381          * @param jsonResource
382          */
383         public void addVnfResource(String jsonResource) throws JsonDecomposingException {
384                 VnfResource vnfResource = null;
385                 vnfResource = DecomposeJsonUtil.jsonToVnfResource(jsonResource);
386                 this.addVnfResource(vnfResource);
387         }
388         /**
389          * Add resource to the list
390          * @param jsonResource
391          */
392         public void addNetworkResource(String jsonResource) throws JsonDecomposingException {
393                 NetworkResource networkResource = null;
394                 networkResource = DecomposeJsonUtil.jsonToNetworkResource(jsonResource);
395                 this.addNetworkResource(networkResource);
396         }
397         /**
398          * Add resource to the list
399          * @param Resource
400          */
401         public void addAllottedResource(String jsonResource) throws JsonDecomposingException {
402                 AllottedResource allottedResource = null;
403                 allottedResource = DecomposeJsonUtil.jsonToAllottedResource(jsonResource);
404                 this.addAllottedResource(allottedResource);
405         }
406         /**
407          * Add resource to the list
408          * @param Resource
409          */
410         public void addConfigResource(String jsonResource) throws JsonDecomposingException {
411                 ConfigResource configResource = null;
412                 configResource = DecomposeJsonUtil.jsonToConfigResource(jsonResource);
413                 this.addConfigResource(configResource);
414         }
415         /**
416          * Given a ResourceDecomposition (subclass) object, locate it in the Service Decomposition by its unique ID, and replace the current version with the new one.
417          * This method should support concurrency control via an auto-incrementing field in the ResourceDecomposition class.
418          * @param newResource
419          * @return TRUE if replacement was a success
420          */
421         public boolean replaceResource(Resource newResource){
422                 List resources = getResourceList(newResource);
423
424                 boolean result = false;
425                 for (Resource resource : (Iterable<Resource>) resources) {
426                         System.out.println("resource found");
427                         if (resource.resourceType == newResource.resourceType) {
428                                 System.out.println("resource type matches");
429                                 if (resource.getResourceId().equalsIgnoreCase(newResource.getResourceId())) {
430                                         System.out.println("resource id matches");
431                                         //returns TRUE if replacement is a success
432                                         result = Collections.replaceAll(resources, resource, newResource);
433                                 }
434                         }
435                 }
436                 return result;
437         }
438
439         /**
440          * Given a ResourceDecomposition as a JSON string, locate it in the Service Decomposition by its unique ID,
441          *  and replace the current version with the new one.
442          * @param jsonString
443          * @return
444          */
445         public boolean replaceResource(String jsonString){
446                 //TODO: define unique ID for the Resource!
447                 return false;
448         }
449
450         /**
451          *  Given a resource object ID, locate it in the Service Decomposition by its unique ID, and delete it.
452          * @param resource
453          * @return TRUE if delete was a success
454          */
455         public boolean deleteResource(Resource resource){
456                 List serviceResourceList = getResourceList(resource);
457                 for (Resource item : (Iterable<Resource>) serviceResourceList) {
458                         if (item.resourceType == resource.resourceType) {
459                                 if (item.getResourceId().equalsIgnoreCase(resource.getResourceId())) {
460                                         //returns TRUE if replacement is a success
461                                         return serviceResourceList.remove(resource);
462                                 }
463                         }
464                 }
465
466                 return false;
467         }
468
469         /**
470          * Generic method to get List of Resource objects based on input resource's resourceType
471          * @param resource
472          * @return List matching the resourceType of resource
473          */
474         public List getResourceList(Resource resource) {
475                 List resourceList;
476                 switch(resource.getResourceType()) {
477                         case VNF:
478                                 resourceList = getVnfResources();
479                                 break;
480                         case NETWORK:
481                                 resourceList = getNetworkResources();
482                                 break;
483                         case ALLOTTED_RESOURCE:
484                                 resourceList = getAllottedResources();
485                                 break;
486                         case CONFIGURATION:
487                                 resourceList = getConfigResources();
488                                 break;
489                         default:
490                                 resourceList = new ArrayList<>();
491                 }
492                 return resourceList;
493         }
494
495         /**
496          * Generic method to set List of ResourceDecomposition objects
497          * @param resources
498          * @return
499          */
500         public boolean setResourceList(List<Resource> resources){
501                 if(resources != null && !resources.isEmpty()) {
502                         //create resource based upon type
503                         switch (resources.get(0).resourceType) {
504                                 case VNF:
505                                         this.setVnfResources((List<VnfResource>)(List<?>)resources);
506                                         break;
507                                 case NETWORK:
508                                         this.setNetworkResources((List<NetworkResource>)(List<?>)resources);
509                                     break;
510                                 case ALLOTTED_RESOURCE:
511                                         this.setAllottedResources((List<AllottedResource>)(List<?>)resources);
512                                     break;
513                                 case CONFIGURATION:
514                                         this.setConfigResources((List<ConfigResource>)(List<?>)resources);
515                                     break;
516                                 default:
517                                      throw new IllegalArgumentException("Invalid resource type: " + resources.get(0).resourceType);
518                         }
519                 }
520
521                 return false;
522         }
523
524         /**
525          *
526          * This method locates and returns a resource in a given
527          * Service Decomposition object by its unique resource id.
528          * Returns null if resource doesn't exist.
529          *
530          * @param resourceId - id of the resource
531          * @return resource
532          */
533         @JsonIgnore
534         public Resource getServiceResource(String resourceId){
535                 List<Resource> resources = getServiceResources();
536                 for (Resource resource : resources) {
537                         if (resource.getResourceId().equalsIgnoreCase(resourceId)) {
538                                 //match
539                                 return resource;
540                         }
541                 }
542                 return null;
543         }
544 }