642843a0c6b9819e34e8e79427af7b1e35c365b0
[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 - 2019 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.JsonIgnore;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.google.common.collect.ImmutableMap;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.vid.job.JobAdapter;
28 import org.onap.vid.job.JobType;
29 import org.onap.vid.model.Action;
30 import org.onap.vid.mso.model.ModelInfo;
31
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36
37 public abstract class BaseResource implements JobAdapter.AsyncJobRequest {
38
39         protected String instanceId;
40
41         protected ModelInfo modelInfo;
42
43         protected String instanceName;
44
45         protected Action action;
46
47         protected String lcpCloudRegionId;
48
49         protected String tenantId;
50
51         protected List<Map<String, String>> instanceParams;
52
53         protected boolean rollbackOnFailure;
54
55         protected String trackById;
56
57         protected Boolean isFailed;
58
59         protected String statusMessage;
60
61         protected Integer position;
62
63
64         private static final Map<String, Action> actionStingToEnumMap = ImmutableMap.<String, Action>builder()
65                         .put("Delete", Action.Delete)
66                         .put("Create", Action.Create)
67                         .put("None", Action.None)
68                         .put("Update_Delete", Action.Delete)
69                         .put("None_Delete", Action.Delete)
70                         .put("Resume", Action.Resume)
71                         .put("Upgrade", Action.Upgrade)
72                         .build();
73
74
75         protected BaseResource(@JsonProperty("modelInfo") ModelInfo modelInfo,
76                 @JsonProperty("instanceName") String instanceName,
77                 @JsonProperty("action") String action,
78                 @JsonProperty("lcpCloudRegionId") String lcpCloudRegionId,
79                 @JsonProperty("legacyRegion") String legacyRegion,
80                 @JsonProperty("tenantId") String tenantId,
81                 @JsonProperty("instanceParams") List<Map<String, String>> instanceParams,
82                 @JsonProperty("rollbackOnFailure") boolean rollbackOnFailure,
83                 @JsonProperty("instanceId") String instanceId,
84                 @JsonProperty("trackById") String trackById,
85                 @JsonProperty("isFailed") Boolean isFailed,
86                 @JsonProperty("statusMessage") String statusMessage,
87                 @JsonProperty("position") Integer position) {
88                 this.modelInfo = modelInfo;
89                 this.modelInfo.setModelType(getModelType());
90                 this.rollbackOnFailure = rollbackOnFailure;
91                 this.instanceName = StringUtils.defaultString(instanceName, "");
92                 this.action = actionStringToEnum(action);
93                 this.lcpCloudRegionId = StringUtils.isNotEmpty(legacyRegion) ? legacyRegion : lcpCloudRegionId;
94                 this.tenantId = tenantId;
95                 this.instanceParams = instanceParams;
96                 this.instanceId = instanceId;
97                 this.trackById = trackById;
98                 this.isFailed = isFailed!= null ? isFailed: false;
99                 this.statusMessage = statusMessage;
100                 this.position = position;
101         }
102
103         private Action actionStringToEnum(String actionAsString) {
104                 return actionStingToEnumMap.get(actionAsString);
105         }
106
107         public ModelInfo getModelInfo() {
108                 return modelInfo;
109         }
110
111         public String getInstanceName() {
112                 return instanceName;
113         }
114
115         public Action getAction() {
116                 return (action == null ? Action.Create : action);
117         }
118
119         public String getLcpCloudRegionId() {
120                 return lcpCloudRegionId;
121         }
122
123         public String getTenantId() {
124                 return tenantId;
125         }
126
127         public List<Map<String, String>> getInstanceParams() {
128                 return instanceParams == null ? Collections.emptyList() : instanceParams;
129         }
130
131         public boolean isRollbackOnFailure() { return rollbackOnFailure; }
132
133         public String getInstanceId() {
134                 return instanceId;
135         }
136
137         protected abstract String getModelType();
138
139         public String getTrackById() {
140                 return trackById;
141         }
142
143         public void setTrackById(String trackById) {
144                 this.trackById = trackById;
145         }
146
147         public Boolean getIsFailed() {
148                 return isFailed;
149         }
150
151         public void setIsFailed(Boolean isFailed) {
152                 this.isFailed = isFailed;
153         }
154
155         public void setInstanceId(String instanceId) {
156                 this.instanceId = instanceId;
157         }
158
159         public void setAction(Action action) {
160                 this.action = action;
161         }
162
163         public String getStatusMessage() {
164                 return statusMessage;
165         }
166
167         public void setStatusMessage(String statusMessage) {
168                 this.statusMessage = statusMessage;
169         }
170
171         public Integer getPosition() {
172                 return position;
173         }
174
175         public void setPosition(Integer position) {
176                 this.position = position;
177         }
178
179         @JsonIgnore
180         public abstract Collection<? extends BaseResource> getChildren();
181
182         @JsonIgnore
183         public abstract JobType getJobType();
184 }