2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.mso.bpmn.core.domain;
\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
30 import com.fasterxml.jackson.annotation.JsonIgnore;
\r
31 import com.fasterxml.jackson.annotation.JsonProperty;
\r
32 import com.fasterxml.jackson.annotation.JsonRootName;
\r
34 import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil;
\r
35 import org.openecomp.mso.bpmn.core.json.JsonDecomposingException;
\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
47 @JsonRootName(value = "serviceResources")
\r
48 //@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)
\r
49 public class ServiceDecomposition extends JsonWrapper implements Serializable {
\r
51 private static final long serialVersionUID = 1L;
\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
67 public ServiceDecomposition () {
\r
71 //GET and SET section
\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
77 public ModelInfo getModelInfo() {
\r
80 public void setModelInfo(ModelInfo modelInfo) {
\r
81 this.modelInfo = modelInfo;
\r
83 public ServiceInstance getServiceInstance() {
\r
84 return serviceInstance;
\r
86 public void setServiceInstance(ServiceInstance serviceInstance) {
\r
87 this.serviceInstance = serviceInstance;
\r
89 public List<VnfResource> getServiceVnfs() {
\r
90 return vnfResources;
\r
92 public void setServiceVnfs(List<VnfResource> vnfResources) {
\r
93 this.vnfResources = vnfResources;
\r
95 public List<NetworkResource> getServiceNetworks() {
\r
96 return networkResources;
\r
98 public void setServiceNetworks(List<NetworkResource> networkResources) {
\r
99 this.networkResources = networkResources;
\r
101 public List<AllottedResource> getServiceAllottedResources() {
\r
102 return allottedResources;
\r
104 public void setServiceAllottedResources(List<AllottedResource> allottedResources) {
\r
105 this.allottedResources = allottedResources;
\r
107 public String getServiceType() {
\r
108 return serviceType;
\r
111 public void setServiceType(String serviceType) {
\r
112 this.serviceType = serviceType;
\r
115 public String getServiceRole() {
\r
116 return serviceRole;
\r
119 public void setServiceRole(String serviceRole) {
\r
120 this.serviceRole = serviceRole;
\r
129 * This method returns one combined list of Resources of All Types
\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
138 if(this.getServiceNetworks() != null){
\r
139 serviceResources.addAll(this.getServiceNetworks());
\r
141 if(this.getServiceVnfs() != null){
\r
142 serviceResources.addAll(this.getServiceVnfs());
\r
144 return serviceResources;
\r
148 * This method returns String representation of one combined list of Resources of All Types
\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
161 * Returns a JSON list of all Network Resource structures (i.e. the serialized NetworkDecomposition objects).
\r
165 public String getServiceNetworksJson(){
\r
166 return listToJson(this.getServiceNetworks());
\r
169 * Returns a JSON list of all VnfResource structures (i.e. the serialized VnfResource objects).
\r
173 public String getServiceVnfsJson(){
\r
174 return listToJson(this.getServiceVnfs());
\r
177 * Returns a JSON list of all Allotted Resource structures (i.e. the serialized AllottedResource objects).
\r
181 public String getServiceAllottedResourcesJson(){
\r
182 return listToJson(this.getServiceAllottedResources());
\r
185 //TODO - define Resource Object ID
\r
187 public String getVnfResource(String resourceObjectId) {
\r
189 for (Resource resource : getServiceResources()) {
\r
190 //resource.getModelInfo().getModelInvariantId();
\r
192 if ("extracted information".equals(resourceObjectId)) {
\r
193 return resource.toJsonString();
\r
199 //Methods to add Resource to the list
\r
201 * Add VNF resource to the list
\r
202 * @param vnfResource
\r
204 public void addVnfResource(Resource vnfResource) {
\r
205 if (vnfResources == null){
\r
206 vnfResources = new ArrayList<>();
\r
208 this.vnfResources.add((VnfResource)vnfResource);
\r
211 * Add Network resource to the list
\r
212 * @param networkResource
\r
214 public void addNetworkResource(Resource networkResource) {
\r
215 if (networkResources == null){
\r
216 networkResources = new ArrayList<>();
\r
218 this.networkResources.add((NetworkResource)networkResource);
\r
221 * Add Allotted resource to the list
\r
222 * @param allottedResource
\r
224 public void addAllottedResource(Resource allottedResource) {
\r
225 if (allottedResources == null){
\r
226 allottedResources = new ArrayList<>();
\r
228 this.allottedResources.add((AllottedResource)allottedResource);
\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
237 public void addResource(Resource resource) {
\r
238 //create resource based upon type
\r
239 switch (resource.resourceType) {
\r
241 this.addVnfResource(resource);
\r
244 this.addNetworkResource(resource);
\r
246 case ALLOTTED_RESOURCE:
\r
247 this.addAllottedResource(resource);
\r
250 throw new IllegalArgumentException("Invalid resource type: " + resource.resourceType);
\r
255 * Add resource to the list
\r
256 * @param jsonResource
\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
264 * Add resource to the list
\r
265 * @param jsonResource
\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
273 * Add resource to the list
\r
274 * @param jsonResource
\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
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
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
302 //set updated list into ServiceDecomposition
\r
303 this.setResourceList(serviceResources);
\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
313 public boolean replaceResource(String jsonString){
\r
314 //TODO: define unique ID for the Resource!
\r
319 * Given a resource object ID, locate it in the Service Decomposition by its unique ID, and delete it.
\r
321 * @return TRUE if delete was a success
\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
338 * Generic method to set List of ResourceDecomposition objects
\r
342 public boolean setResourceList(List<Resource> resources){
\r
343 //create resource based upon type
\r
344 switch (resources.get(0).resourceType) {
\r
346 this.setServiceVnfs((List<VnfResource>)(List<?>)resources);
\r
349 this.setServiceNetworks((List<NetworkResource>)(List<?>)resources);
\r
351 case ALLOTTED_RESOURCE:
\r
352 this.setServiceAllottedResources((List<AllottedResource>)(List<?>)resources);
\r
355 throw new IllegalArgumentException("Invalid resource type: " + resources.get(0).resourceType);
\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
367 * @param resourceId - id of the resource
\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