aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java
blob: 077e5726d7e65fd437d2d2e4108142397e8ebe1a (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

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;
import org.openecomp.mso.bpmn.core.json.JsonDecomposingException;




/**
 * 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;

	@JsonProperty("modelInfo")
	private ModelInfo modelInfo;
	@JsonProperty("serviceType")
	private String serviceType;
	@JsonProperty("serviceRole")
	private String serviceRole;
	private ServiceInstance serviceInstance;
	private Request request;
	private Customer customer;
	private String callbackURN;
	private String sdncVersion;
	@JsonProperty("project")
	private Project project;
	@JsonProperty("owningEntity")
	private OwningEntity owningEntity;
	@JsonProperty("vnfResource")
	private List <VnfResource>  vnfResources;
	@JsonProperty("networkResource")
	private List <NetworkResource>  networkResources;
	@JsonProperty("allottedResource")
	private List <AllottedResource>  allottedResources;
	@JsonProperty("configResource")
	private List <ConfigResource>  configResources;

	public ServiceDecomposition () {
		super();
	}

	public ServiceDecomposition (String catalogRestOutput) throws JsonDecomposingException {
		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.configResources = serviceDecomposition.getServiceConfigResources();
	}

	/**
	 * Constructor taking Catalog DB Adapter REST output (serviceResources model) + service Instance ID
	 * @param catalogRestOutput
	 * @param serviceInstanceId
	 */
	public ServiceDecomposition (String catalogRestOutput, String serviceInstanceId) throws JsonDecomposingException {
		ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.jsonToServiceDecomposition(catalogRestOutput);
		this.modelInfo = serviceDecomposition.getModelInfo();
		this.vnfResources = serviceDecomposition.getServiceVnfs();
		this.allottedResources = serviceDecomposition.getServiceAllottedResources();
		this.configResources = serviceDecomposition.getServiceConfigResources();
		this.networkResources = serviceDecomposition.getServiceNetworks();

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

	/**
	 * 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 Project getProject() {
		return project;
	}
	public OwningEntity getOwningEntity() {
		return owningEntity;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public void setOwningEntity(OwningEntity owningEntity) {
		this.owningEntity = owningEntity;
	}
	public List<VnfResource> getServiceVnfs() {
		return vnfResources;
	}
	public void setServiceVnfs(List<VnfResource> vnfResources) {
		this.vnfResources = vnfResources;
	}
	public List<NetworkResource> getServiceNetworks() {
		return networkResources;
	}
	public void setServiceConfigs(List<ConfigResource> configResources) {
		this.configResources = configResources;
	}
	public List<ConfigResource> getServiceConfigs() {
		return configResources;
	}
	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 List<ConfigResource> getServiceConfigResources() {
		return configResources;
	}
	public void setServiceConfigResources(List<ConfigResource> configResources) {
		this.configResources = configResources;
	}
	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;
	}
	public Request getRequest() {
		return request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public String getCallbackURN() {
		return callbackURN;
	}

	public void setCallbackURN(String callbackURN) {
		this.callbackURN = callbackURN;
	}

	public String getSdncVersion() {
		return sdncVersion;
	}

	public void setSdncVersion(String sdncVersion) {
		this.sdncVersion = sdncVersion;
	}
	
	//*****

	//*****
	//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());
		}
		if(this.getServiceConfigResources() != null){
			serviceResources.addAll(this.getServiceConfigResources());
		}
		return serviceResources;
	}

	/**
	 * This method returns String representation of one combined list of Resources of All Types
	 */
	@JsonIgnore
	public String getServiceResourcesJsonString() {
		return listToJson((this.getServiceNetworks())) +
			listToJson((this.getServiceVnfs())) +
			listToJson((this.getServiceAllottedResources())) +
			listToJson((this.getServiceConfigResources()));
	}

	/**
	 * 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());
	}
	/**
	 * Returns a JSON list of all Config Resource structures (i.e. the serialized ConfigResource objects).
	 * @return
	 */
	@JsonIgnore
	public String getServiceConfigResourcesJson(){
		return listToJson(this.getServiceConfigResources());
	}

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

        for (Resource resource : getServiceResources()) {
            //resource.getModelInfo().getModelInvariantId();

            if ("extracted information".equals(resourceObjectId)) {
                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<>();
		}
		this.vnfResources.add((VnfResource)vnfResource);
	}
	/**
	 * Add Network resource to the list
	 * @param networkResource
	 */
	public void addNetworkResource(Resource networkResource) {
		if (networkResources == null){
			networkResources = new ArrayList<>();
		}
		this.networkResources.add((NetworkResource)networkResource);
	}
	/**
	 * Add Allotted resource to the list
	 * @param allottedResource
	 */
	public void addAllottedResource(Resource allottedResource) {
		if (allottedResources == null){
			allottedResources = new ArrayList<>();
		}
		this.allottedResources.add((AllottedResource)allottedResource);
	}
	/**
	 * Add Config resource to the list
	 * @param allottedResource
	 */
	public void addConfigResource(Resource configResource) {
		if (configResources == null){
			configResources = new ArrayList<>();
		}
		this.configResources.add((ConfigResource)configResource);
	}
	/**
	 * 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;
		case CONFIGURATION:
			this.addConfigResource(resource);
		    break;
		default:
		     throw new IllegalArgumentException("Invalid resource type: " + resource.resourceType);
		 }
	}

	/**
	 * Add resource to the list
	 * @param jsonResource
	 */
	public void addVnfResource(String jsonResource) throws JsonDecomposingException {
		VnfResource vnfResource = null;
		vnfResource = DecomposeJsonUtil.jsonToVnfResource(jsonResource);
		this.addVnfResource(vnfResource);
	}
	/**
	 * Add resource to the list
	 * @param jsonResource
	 */
	public void addNetworkResource(String jsonResource) throws JsonDecomposingException {
		NetworkResource networkResource = null;
		networkResource = DecomposeJsonUtil.jsonToNetworkResource(jsonResource);
		this.addVnfResource(networkResource);
	}
	/**
	 * Add resource to the list
	 * @param Resource
	 */
	public void addAllottedResource(String jsonResource) throws JsonDecomposingException {
		AllottedResource allottedResource = null;
		allottedResource = DecomposeJsonUtil.jsonToAllottedResource(jsonResource);
		this.addVnfResource(allottedResource);
	}
	/**
	 * Add resource to the list
	 * @param Resource
	 */
	public void addConfigResource(String jsonResource) throws JsonDecomposingException {
		ConfigResource configResource = null;
		configResource = DecomposeJsonUtil.jsonToConfigResource(jsonResource);
		this.addVnfResource(configResource);
	}
	/**
	 * 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 newResource
	 * @return TRUE if replacement was a success
	 */
	public boolean replaceResource(Resource newResource){
		boolean result = false;
		List serviceResources = getServiceResources();
		for (Resource resource : (Iterable<Resource>) serviceResources) {
			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();
		for (Resource item : (Iterable<Resource>) serviceResources) {
			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;
		case CONFIGURATION:
			this.setServiceConfigResources((List<ConfigResource>)(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();
		for (Resource resource : resources) {
			if (resource.getResourceId().equalsIgnoreCase(resourceId)) {
				//match
				return resource;
			}
		}
		return null;
	}

}