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