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