Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / serviceInstantiation / BaseResource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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.vid.model.serviceInstantiation;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24 import com.google.common.collect.ImmutableMap;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.vid.job.JobAdapter;
27 import org.onap.vid.model.Action;
28 import org.onap.vid.mso.model.ModelInfo;
29
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33
34 public abstract class BaseResource implements JobAdapter.AsyncJobRequest {
35
36         protected final String instanceId;
37         protected ModelInfo modelInfo;
38
39         protected String instanceName;
40
41         protected final Action action;
42
43         protected String lcpCloudRegionId;
44
45         protected String tenantId;
46
47         protected List<Map<String, String>> instanceParams;
48
49         protected boolean rollbackOnFailure;
50
51         private static final Map<String, Action> actionStingToEnumMap = ImmutableMap.of(
52                         "Delete", Action.Delete,
53                         "Create", Action.Create,
54                         "None", Action.None,
55                         "Update_Delete", Action.Delete,
56                         "None_Delete", Action.Delete
57         );
58
59
60         protected BaseResource(@JsonProperty("modelInfo") ModelInfo modelInfo,
61                                                    @JsonProperty("instanceName") String instanceName,
62                                                    @JsonProperty("action") String action,
63                                                    @JsonProperty("lcpCloudRegionId") String lcpCloudRegionId,
64                                                    @JsonProperty("legacyRegion") String legacyRegion,
65                                                    @JsonProperty("tenantId") String tenantId,
66                                                    @JsonProperty("instanceParams") List<Map<String, String>> instanceParams,
67                                                    @JsonProperty("rollbackOnFailure") boolean rollbackOnFailure,
68                                                    @JsonProperty("instanceId") String instanceId) {
69                 this.modelInfo = modelInfo;
70                 this.modelInfo.setModelType(getModelType());
71                 this.rollbackOnFailure = rollbackOnFailure;
72                 this.instanceName = StringUtils.defaultString(instanceName, "");;
73                 this.action = actionStringToEnum(action);
74                 this.lcpCloudRegionId = StringUtils.isNotEmpty(legacyRegion) ? legacyRegion : lcpCloudRegionId;
75                 this.tenantId = tenantId;
76                 this.instanceParams = instanceParams;
77                 this.instanceId = instanceId;
78         }
79
80         private Action actionStringToEnum(String actionAsString) {
81                 return actionStingToEnumMap.get(actionAsString);
82         }
83
84         public ModelInfo getModelInfo() {
85                 return modelInfo;
86         }
87
88         public String getInstanceName() {
89                 return instanceName;
90         }
91
92         public Action getAction() {
93                 return action;
94         }
95
96         public String getLcpCloudRegionId() {
97                 return lcpCloudRegionId;
98         }
99
100         public String getTenantId() {
101                 return tenantId;
102         }
103
104         public List<Map<String, String>> getInstanceParams() {
105                 return instanceParams == null ? Collections.emptyList() : instanceParams;
106         }
107
108     public boolean isRollbackOnFailure() { return rollbackOnFailure; }
109
110         public String getInstanceId() {
111                 return instanceId;
112         }
113
114         protected abstract String getModelType();
115 }