summaryrefslogtreecommitdiffstats
path: root/so-nssmf-adapter-application/src/main/java/org/onap/so/adapters/nssmf/manager/impl/BaseNssmfManager.java
blob: 064f7b1e42e8e88c4b175b61fb475335185ce6de (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
 #
 # 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.onap.so.adapters.nssmf.manager.impl;

import org.onap.so.adapters.nssmf.enums.ActionType;
import org.onap.so.adapters.nssmf.enums.ExecutorType;
import org.onap.so.adapters.nssmf.enums.HttpMethod;
import org.onap.so.adapters.nssmf.enums.SelectionType;
import org.onap.so.adapters.nssmf.config.NssmfAdapterConfig;
import org.onap.so.adapters.nssmf.consts.NssmfAdapterConsts;
import org.onap.so.adapters.nssmf.entity.NssmfUrlInfo;
import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
import org.onap.so.adapters.nssmf.entity.RestResponse;
import org.onap.so.adapters.nssmf.manager.NssmfManager;
import org.onap.so.adapters.nssmf.util.RestUtil;
import org.onap.so.beans.nsmf.ActDeActNssi;
import org.onap.so.beans.nsmf.DeAllocateNssi;
import org.onap.so.beans.nsmf.EsrInfo;
import org.onap.so.beans.nsmf.QuerySubnetCapability;
import org.onap.so.beans.nsmf.NetworkType;
import org.onap.so.beans.nsmf.NssmfAdapterNBIRequest;
import org.onap.so.beans.nsmf.ServiceInfo;
import org.onap.so.db.request.beans.ResourceOperationStatus;
import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;

public abstract class BaseNssmfManager implements NssmfManager {

    private static final Logger logger = LoggerFactory.getLogger(BaseNssmfManager.class);

    protected RestUtil restUtil;

    protected ResourceOperationStatusRepository repository;

    protected NssmfAdapterConfig adapterConfig;

    protected ActionType actionType;

    protected EsrInfo esrInfo;

    protected String nssmfUrl;

    protected HttpMethod httpMethod;

    protected String initStatus;

    protected ServiceInfo serviceInfo;

    protected RestResponse restResponse;

    private ExecutorType executorType = ExecutorType.INTERNAL;

    private Map<String, String> params = new HashMap<>(); // request params

    @Override
    public RestResponse allocateNssi(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {

        this.params.clear();
        this.urlHandler();
        String requestBody = wrapAllocateReqBody(nbiRequest);

        this.restResponse = sendRequest(requestBody);

        this.afterRequest();

        return restResponse;
    }

    protected abstract String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;

    @Override
    public RestResponse modifyNssi(NssmfAdapterNBIRequest modifyRequest) throws ApplicationException {
        this.params.clear();
	if(this.esrInfo.getNetworkType().equals(NetworkType.CORE))
        {
        //this.params.put("sliceProfileId", modifyRequest.getAllocateCnNssi().getSliceProfile().getSliceProfileId());
	this.params.put("nssiId", modifyRequest.getAllocateCnNssi().getNssiId());
        }
        this.urlHandler();
        String requestBody = wrapModifyReqBody(modifyRequest);

        this.restResponse = sendRequest(requestBody);

        this.afterRequest();

        return restResponse;
    }

    protected abstract String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;

    @Override
    public RestResponse deAllocateNssi(NssmfAdapterNBIRequest nbiRequest, String sliceId) throws ApplicationException {
        this.params.clear();
        this.params.put("sliceProfileId", sliceId);

        this.urlHandler();

        String reqBody = wrapDeAllocateReqBody(nbiRequest.getDeAllocateNssi());

        this.restResponse = sendRequest(reqBody);

        this.afterRequest();

        return restResponse;
    }

    protected abstract String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException;

    protected abstract String wrapReqBody(Object object) throws ApplicationException;

    @Override
    public RestResponse activateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
        this.params.clear();
        this.params.put("snssai", snssai);

        this.urlHandler();

        String reqBody = wrapActDeActReqBody(nbiRequest.getActDeActNssi());

        this.restResponse = sendRequest(reqBody);

        this.afterRequest();

        return restResponse;
    }

    @Override
    public RestResponse deActivateNssi(NssmfAdapterNBIRequest nbiRequest, String snssai) throws ApplicationException {
        return activateNssi(nbiRequest, snssai);
    }

    protected abstract String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException;

    @Override
    public RestResponse queryJobStatus(NssmfAdapterNBIRequest jobReq, String jobId) throws ApplicationException {
        this.params.clear();
        this.params.put("jobId", jobId);
        if (jobReq.getResponseId() != null) {
            this.params.put("responseId", jobReq.getResponseId());
        }
        this.urlHandler();

        /**
         * find by jobId and nsiId jobId -> OperationId nsiId -> ServiceId serviceUuid -> resourceTemplateUUID
         */
        ResourceOperationStatus status =
                getOperationStatus(serviceInfo.getNsiId(), jobId, serviceInfo.getServiceUuid());

        logger.info("ResourceOperationStatus = {}", status);
        this.restResponse = doQueryJobStatus(status);

        afterQueryJobStatus(status);
        return restResponse;
    }

    protected abstract RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException;


    protected abstract void afterQueryJobStatus(ResourceOperationStatus status);

    private ResourceOperationStatus getOperationStatus(String nsiId, String jobId, String serviceUuid) {

        logger.info("Query operations: nsiId = [{}], jobId = [{}], serviceUuid = [{}]", nsiId, jobId, serviceUuid);

        List<ResourceOperationStatus> resourceOperationStatuses =
                repository.findByServiceIdAndOperationId(nsiId, jobId);

        logger.info("resourceOperationStatuses = {}", resourceOperationStatuses);
        if (resourceOperationStatuses.size() == 0) {
            return null;
        }
        return resourceOperationStatuses.get(0);
    }

    @Override
    public RestResponse queryNSSISelectionCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
        SelectionType res = doQueryNSSISelectionCapability();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("selection", res.name());
        RestResponse restResponse = new RestResponse();
        restResponse.setStatus(200);
        restResponse.setResponseContent(marshal(hashMap));
        return restResponse;
    }

    protected abstract SelectionType doQueryNSSISelectionCapability();

    @Override
    public RestResponse querySubnetCapability(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
        this.params.clear();
        this.urlHandler();

        return doQuerySubnetCapability(nbiRequest.getSubnetCapabilityQuery());
    }

    protected abstract RestResponse doQuerySubnetCapability(QuerySubnetCapability req) throws ApplicationException;

    /**
     * send request to nssmf
     * 
     * @param content request body
     * @return response
     * @throws ApplicationException
     */
    protected abstract RestResponse sendRequest(String content) throws ApplicationException;

    /**
     * handle the url before request to nssmf, include get the nssmf request url, replace the path variable
     */
    private void urlHandler() {
        NssmfUrlInfo nssmfUrlInfo =
                NssmfAdapterConsts.getNssmfUrlInfo(this.executorType, this.esrInfo.getNetworkType(), actionType);
        if (nssmfUrlInfo != null) {
            this.nssmfUrl = nssmfUrlInfo.getUrl();
            this.httpMethod = nssmfUrlInfo.getHttpMethod();
            this.nssmfUrl = nssmfUrl.replaceAll("\\{apiVersion}", getApiVersion());
            this.params.forEach((k, v) -> this.nssmfUrl = this.nssmfUrl.replaceAll("\\{" + k + "}", v));
        }
    }

    /**
     * after request
     */
    protected abstract void afterRequest() throws ApplicationException;

    protected abstract String getApiVersion();

    public RestUtil getRestUtil() {
        return restUtil;
    }

    public BaseNssmfManager setEsrInfo(EsrInfo esrInfo) {
        this.esrInfo = esrInfo;
        return this;
    }

    public BaseNssmfManager setExecutorType(ExecutorType executorType) {
        this.executorType = executorType;
        return this;
    }

    public BaseNssmfManager setRestUtil(RestUtil restUtil) {
        this.restUtil = restUtil;
        return this;
    }

    public BaseNssmfManager setActionType(ActionType actionType) {
        this.actionType = actionType;
        return this;
    }

    public BaseNssmfManager setRepository(ResourceOperationStatusRepository repository) {
        this.repository = repository;
        return this;
    }

    public BaseNssmfManager setServiceInfo(ServiceInfo serviceInfo) {
        this.serviceInfo = serviceInfo;
        return this;
    }

    public BaseNssmfManager setInitStatus(String initStatus) {
        this.initStatus = initStatus;
        return this;
    }

    public BaseNssmfManager setAdapterConfig(NssmfAdapterConfig adapterConfig) {
        this.adapterConfig = adapterConfig;
        return this;
    }
}