Expect None_Upgrade action for VNF upgrade
[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                         .put("None_Upgrade", Action.Upgrade)
73                         .build();
74
75
76         protected BaseResource(@JsonProperty("modelInfo") ModelInfo modelInfo,
77                 @JsonProperty("instanceName") String instanceName,
78                 @JsonProperty("action") String action,
79                 @JsonProperty("lcpCloudRegionId") String lcpCloudRegionId,
80                 @JsonProperty("legacyRegion") String legacyRegion,
81                 @JsonProperty("tenantId") String tenantId,
82                 @JsonProperty("instanceParams") List<Map<String, String>> instanceParams,
83                 @JsonProperty("rollbackOnFailure") boolean rollbackOnFailure,
84                 @JsonProperty("instanceId") String instanceId,
85                 @JsonProperty("trackById") String trackById,
86                 @JsonProperty("isFailed") Boolean isFailed,
87                 @JsonProperty("statusMessage") String statusMessage,
88                 @JsonProperty("position") Integer position) {
89                 this.modelInfo = modelInfo;
90                 this.modelInfo.setModelType(getModelType());
91                 this.rollbackOnFailure = rollbackOnFailure;
92                 this.instanceName = StringUtils.defaultString(instanceName, "");
93                 this.action = actionStringToEnum(action);
94                 this.lcpCloudRegionId = StringUtils.isNotEmpty(legacyRegion) ? legacyRegion : lcpCloudRegionId;
95                 this.tenantId = tenantId;
96                 this.instanceParams = instanceParams;
97                 this.instanceId = instanceId;
98                 this.trackById = trackById;
99                 this.isFailed = isFailed!= null ? isFailed: false;
100                 this.statusMessage = statusMessage;
101                 this.position = position;
102         }
103
104         private Action actionStringToEnum(String actionAsString) {
105                 return actionStingToEnumMap.get(actionAsString);
106         }
107
108         public ModelInfo getModelInfo() {
109                 return modelInfo;
110         }
111
112         public String getInstanceName() {
113                 return instanceName;
114         }
115
116         public Action getAction() {
117                 return (action == null ? Action.Create : action);
118         }
119
120         public String getLcpCloudRegionId() {
121                 return lcpCloudRegionId;
122         }
123
124         public String getTenantId() {
125                 return tenantId;
126         }
127
128         public List<Map<String, String>> getInstanceParams() {
129                 return instanceParams == null ? Collections.emptyList() : instanceParams;
130         }
131
132         public boolean isRollbackOnFailure() { return rollbackOnFailure; }
133
134         public String getInstanceId() {
135                 return instanceId;
136         }
137
138         protected abstract String getModelType();
139
140         public String getTrackById() {
141                 return trackById;
142         }
143
144         public void setTrackById(String trackById) {
145                 this.trackById = trackById;
146         }
147
148         public Boolean getIsFailed() {
149                 return isFailed;
150         }
151
152         public void setIsFailed(Boolean isFailed) {
153                 this.isFailed = isFailed;
154         }
155
156         public void setInstanceId(String instanceId) {
157                 this.instanceId = instanceId;
158         }
159
160         public void setAction(Action action) {
161                 this.action = action;
162         }
163
164         public String getStatusMessage() {
165                 return statusMessage;
166         }
167
168         public void setStatusMessage(String statusMessage) {
169                 this.statusMessage = statusMessage;
170         }
171
172         public Integer getPosition() {
173                 return position;
174         }
175
176         public void setPosition(Integer position) {
177                 this.position = position;
178         }
179
180         @JsonIgnore
181         public abstract Collection<? extends BaseResource> getChildren();
182
183         @JsonIgnore
184         public abstract JobType getJobType();
185 }