aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java
blob: 4bee1154119be48f98720c3777b47bce2ce94860 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package org.openecomp.mso.bpmn.core.domain;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

import org.json.JSONObject;
import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil;



/**
 * Service Decomposition Structure
 * This Java object contains service information:
 * - Service model info
 * - Service type and role
 * - list of VNF resource's decompositon
 * - list of network resource's decompositon
 * - list of allotted resource's decompositon
 */
@JsonRootName(value = "serviceResources")
//@JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)
public class ServiceDecomposition extends JsonWrapper implements Serializable {

	private static final long serialVersionUID = 1L;
	DecomposeJsonUtil jsonUtils = new DecomposeJsonUtil();

	@JsonProperty("modelInfo")
	private ModelInfo modelInfo;
	@JsonProperty("serviceType")
	private String serviceType;
	@JsonProperty("serviceRole")
	private String serviceRole;
	private ServiceInstance serviceInstance;
	@JsonProperty("vnfResource")
	private List <VnfResource>  vnfResources;
	@JsonProperty("networkResource")
	private List <NetworkResource>  networkResources;
	@JsonProperty("allottedResource")
	private List <AllottedResource>  allottedResources;

	public ServiceDecomposition () {
		super();
	}

	public ServiceDecomposition (String catalogRestOutput) {

		ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.JsonToServiceDecomposition(catalogRestOutput);
		this.modelInfo = serviceDecomposition.getModelInfo();
		this.vnfResources = serviceDecomposition.getServiceVnfs();
		this.allottedResources = serviceDecomposition.getServiceAllottedResources();
		this.networkResources = serviceDecomposition.getServiceNetworks();
		this.serviceRole = serviceDecomposition.getServiceRole();
		this.serviceType = serviceDecomposition.getServiceType();
	}

	/**
	 * Constructor taking Catalog DB Adapter REST output (serviceResources model) + service Instance ID
	 * @param catalogRestOutput
	 * @param serviceInstanceId
	 */
	public ServiceDecomposition (String catalogRestOutput, String serviceInstanceId) {

		ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.JsonToServiceDecomposition(catalogRestOutput);
		this.modelInfo = serviceDecomposition.getModelInfo();
		this.vnfResources = serviceDecomposition.getServiceVnfs();
		this.allottedResources = serviceDecomposition.getServiceAllottedResources();
		this.networkResources = serviceDecomposition.getServiceNetworks();

		this.serviceRole = serviceDecomposition.getServiceRole();
		this.serviceType = serviceDecomposition.getServiceType();
		
		this.serviceInstance = new ServiceInstance();
		this.serviceInstance.setInstanceId(serviceInstanceId);
	}

	/**
	 * Constructor taking a Service Decomposition JSON serialization
	 * @param catalogRestOutput
	 * @param serviceInstanceId
	 */
	public ServiceDecomposition (JSONObject jsonServiceDecomposition, String serviceInstanceId) {
		//TODO provide constructor implementation

	}

	//*****
	//GET and SET section
	/**
	 * Return just the service model portion of the Service Decomposition as a Java object.
	 * The service model object should support retrieval as JSON string that is formatted correctly for sending serviceModelInfo to Building Blocks.
	 * @return
	 */
	public ModelInfo getModelInfo() {
		return modelInfo;
	}
	public void setModelInfo(ModelInfo modelInfo) {
		this.modelInfo = modelInfo;
	}
	public ServiceInstance getServiceInstance() {
		return serviceInstance;
	}
	public void setServiceInstance(ServiceInstance serviceInstance) {
		this.serviceInstance = serviceInstance;
	}
	public List<VnfResource> getServiceVnfs() {
		return vnfResources;
	}
	public void setServiceVnfs(List<VnfResource> vnfResources) {
		this.vnfResources = vnfResources;
	}
	public List<NetworkResource> getServiceNetworks() {
		return networkResources;
	}
	public void setServiceNetworks(List<NetworkResource> networkResources) {
		this.networkResources = networkResources;
	}
	public List<AllottedResource> getServiceAllottedResources() {
		return allottedResources;
	}
	public void setServiceAllottedResources(List<AllottedResource> allottedResources) {
		this.allottedResources = allottedResources;
	}
	public String getServiceType() {
		return serviceType;
	}

	public void setServiceType(String serviceType) {
		this.serviceType = serviceType;
	}

	public String getServiceRole() {
		return serviceRole;
	}

	public void setServiceRole(String serviceRole) {
		this.serviceRole = serviceRole;
	}
	//*****

	//*****
	//Access methods


	/**
	 * This method returns one combined list of Resources of All Types
	 * @return
	 */
	@JsonIgnore
	public List<Resource> getServiceResources(){
		ArrayList serviceResources = new ArrayList();
		if(this.getServiceAllottedResources() != null){
			serviceResources.addAll(this.getServiceAllottedResources());
		}
		if(this.getServiceNetworks() != null){
			serviceResources.addAll(this.getServiceNetworks());
		}
		if(this.getServiceVnfs() != null){
			serviceResources.addAll(this.getServiceVnfs());
		}
		return serviceResources;
	}

	/**
	 * This method returns String representation of one combined list of Resources of All Types
	 * @return
	 */
	@JsonIgnore
	public String getServiceResourcesJsonString(){
		StringBuffer serviceResourcesJsonStringBuffer = new StringBuffer();
		serviceResourcesJsonStringBuffer.append(listToJson((this.getServiceNetworks())));
		serviceResourcesJsonStringBuffer.append(listToJson((this.getServiceVnfs())));
		serviceResourcesJsonStringBuffer.append(listToJson((this.getServiceAllottedResources())));
		return serviceResourcesJsonStringBuffer.toString();
	}

	/**
	 * Returns a JSON list of all Network Resource structures (i.e. the serialized NetworkDecomposition objects).
	 * @return
	 */
	@JsonIgnore
	public String getServiceNetworksJson(){
		return listToJson(this.getServiceNetworks());
	}
	/**
	 * Returns a JSON list of all VnfResource structures (i.e. the serialized VnfResource objects).
	 * @return
	 */
	@JsonIgnore
	public String getServiceVnfsJson(){
		return listToJson(this.getServiceVnfs());
	}
	/**
	 * Returns a JSON list of all Allotted Resource structures (i.e. the serialized AllottedResource objects).
	 * @return
	 */
	@JsonIgnore
	public String getServiceAllottedResourcesJson(){
		return listToJson(this.getServiceAllottedResources());
	}

	//TODO - define Resource Object ID
	@JsonIgnore
	public String getVnfResource(String resourceObjectId) {

		Iterator<Resource> iter = getServiceResources().iterator();
		while (iter.hasNext()) {
			Resource resource = iter.next();
			//resource.getModelInfo().getModelInvariantId();

			if (resourceObjectId.equals("extracted information")){
				return resource.toJsonString();
			}
		}
		return "";
	}

	//Methods to add Resource to the list
	/**
	 * Add VNF resource to the list
	 * @param vnfResource
	 */
	public void addVnfResource(Resource vnfResource) {
		if (vnfResources == null){
			vnfResources = new ArrayList<VnfResource>();
		}
		this.vnfResources.add((VnfResource)vnfResource);
	}
	/**
	 * Add Network resource to the list
	 * @param networkResource
	 */
	public void addNetworkResource(Resource networkResource) {
		if (networkResources == null){
			networkResources = new ArrayList<NetworkResource>();
		}
		this.networkResources.add((NetworkResource)networkResource);
	}
	/**
	 * Add Allotted resource to the list
	 * @param allottedResource
	 */
	public void addAllottedResource(Resource allottedResource) {
		if (allottedResources == null){
			allottedResources = new ArrayList<AllottedResource>();
		}
		this.allottedResources.add((AllottedResource)allottedResource);
	}

	/**
	 * Add resource to the list
	 * Given a ResourceDecomposition (subclass) object, add it to the Service Decomposition (in the appropriate category, e.g. as a VNF, Network, or Allotted Resource).
	 * As dependencies are not currently supported, add it to the end of any ordered lists.
	 * @param Resource
	 */
	public void addResource(Resource resource) {
		//create resource based upon type
		switch (resource.resourceType) {
		case VNF:
			this.addVnfResource(resource);
			break;
		case NETWORK:
			this.addNetworkResource(resource);
		     break;
		case ALLOTTED_RESOURCE:
			this.addAllottedResource(resource);
		    break;
		default:
		     throw new IllegalArgumentException("Invalid resource type: " + resource.resourceType);
		 }
	}

	/**
	 * Add resource to the list
	 * @param Resource
	 */
	public void addVnfResource(String jsonResource) {
		VnfResource vnfResource = null;
		vnfResource = DecomposeJsonUtil.JsonToVnfResource(jsonResource);
		this.addVnfResource(vnfResource);
	}
	/**
	 * Add resource to the list
	 * @param Resource
	 */
	public void addNetworkResource(String jsonResource) {
		NetworkResource networkResource = null;
		networkResource = DecomposeJsonUtil.JsonToNetworkResource(jsonResource);
		this.addVnfResource(networkResource);
	}
	/**
	 * Add resource to the list
	 * @param Resource
	 */
	public void addAllottedResource(String jsonResource) {
		AllottedResource allottedResource = null;
		allottedResource = DecomposeJsonUtil.JsonToAllottedResource(jsonResource);
		this.addVnfResource(allottedResource);
	}

	/**
	 * Given a ResourceDecomposition (subclass) object, locate it in the Service Decomposition by its unique ID, and replace the current version with the new one.
	 * This method should support concurrency control via an auto-incrementing field in the ResourceDecomposition class.
	 * @param Resource
	 * @return TRUE if replacement was a success
	 */
	public boolean replaceResource(Resource newResource){
		boolean result = false;
		List serviceResources = getServiceResources();
		Iterator<Resource> iter = serviceResources.iterator();
		while (iter.hasNext()) {
			Resource resource = iter.next();
			System.out.println("resource found");
			if (resource.resourceType == newResource.resourceType){
				System.out.println("resource type matches");
				if (resource.getResourceId().equalsIgnoreCase(newResource.getResourceId())){
					System.out.println("resource id matches");
					//returns TRUE if replacement is a success
					result = Collections.replaceAll(serviceResources, resource, newResource);
				}
			}
		}
		//set updated list into ServiceDecomposition
		this.setResourceList(serviceResources);
		return result;
	}

	/**
	 * Given a ResourceDecomposition as a JSON string, locate it in the Service Decomposition by its unique ID,
	 *  and replace the current version with the new one.
	 * @param jsonString
	 * @return
	 */
	public boolean replaceResource(String jsonString){
		//TODO: define unique ID for the Resource!
		return false;
	}

	/**
	 *  Given a resource object ID, locate it in the Service Decomposition by its unique ID, and delete it.
	 * @param Resource
	 * @return TRUE if delete was a success
	 */
	public boolean deleteResource(Resource resource){
		List serviceResources = getServiceResources();
		Iterator<Resource> iter = serviceResources.iterator();
		while (iter.hasNext()) {
			Resource item = iter.next();

			if (item.resourceType == resource.resourceType){
				if (item.getResourceId().equalsIgnoreCase(resource.getResourceId())){
					//returns TRUE if replacement is a success
					return serviceResources.remove(resource);
				}
			}
		}

		return false;
	}

	/**
	 * Generic method to set List of ResourceDecomposition objects
	 * @param resources
	 * @return
	 */
	public boolean setResourceList(List<Resource> resources){
		//create resource based upon type
		switch (resources.get(0).resourceType) {
		case VNF:
			this.setServiceVnfs((List<VnfResource>)(List<?>)resources);
			break;
		case NETWORK:
			this.setServiceNetworks((List<NetworkResource>)(List<?>)resources);
		    break;
		case ALLOTTED_RESOURCE:
			this.setServiceAllottedResources((List<AllottedResource>)(List<?>)resources);
		    break;
		default:
		     throw new IllegalArgumentException("Invalid resource type: " + resources.get(0).resourceType);
		 }

		return false;
	}

	/**
	 *
	 * This method locates and returns a resource in a given
	 * Service Decomposition object by its unique resource id.
	 * Returns null if resource doesn't exist.
	 *
	 * @param resourceId - id of the resource
	 * @return resource
	 */
	@JsonIgnore
	public Resource getServiceResource(String resourceId){
		List<Resource> resources = getServiceResources();
		Iterator<Resource> iter = resources.iterator();
		while (iter.hasNext()){
			Resource resource = iter.next();
			if (resource.getResourceId().equalsIgnoreCase(resourceId)){
				//match
				return resource;
			}
		}
		return null;
	}
}