64ab199bc1045cab33a10e02165e672b31654fe2
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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.so.adapters.nssmf.manager.impl;
22
23 import org.apache.http.Header;
24 import org.apache.http.message.BasicHeader;
25 import org.onap.so.adapters.nssmf.consts.NssmfAdapterConsts;
26 import org.onap.so.adapters.nssmf.entity.RestResponse;
27 import org.onap.so.adapters.nssmf.enums.SelectionType;
28 import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
29 import org.onap.so.beans.nsmf.*;
30 import org.onap.so.db.request.beans.ResourceOperationStatus;
31 import static org.onap.so.adapters.nssmf.enums.JobStatus.PROCESSING;
32 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
33
34 public abstract class InternalNssmfManager extends BaseNssmfManager {
35
36     @Override
37     protected String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
38         return doWrapAllocateReqBody(nbiRequest);
39     }
40
41     protected abstract String doWrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
42
43     @Override
44     protected String wrapReqBody(Object object) throws ApplicationException {
45         NssmfRequest nssmfRequest = new NssmfRequest(serviceInfo, esrInfo.getNetworkType(), object);
46         return marshal(nssmfRequest);
47     }
48
49
50     @Override
51     protected String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException {
52
53         return wrapReqBody(actDeActNssi);
54     }
55
56
57     @Override
58     protected String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException {
59         return wrapReqBody(deAllocateNssi);
60     }
61
62
63     @Override
64     protected RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException {
65         return responseDBStatus(status);
66     }
67
68     private RestResponse responseDBStatus(ResourceOperationStatus status) throws ApplicationException {
69         JobStatusResponse statusResponse = new JobStatusResponse();
70         ResponseDescriptor descriptor = new ResponseDescriptor();
71         if (status == null) {
72             descriptor.setProgress(0);
73             descriptor.setStatus(PROCESSING.name());
74             descriptor.setStatusDescription("Initiating Nssi Instance");
75         } else {
76             descriptor.setStatus(status.getStatus());
77             descriptor.setStatusDescription(status.getStatusDescription());
78             descriptor.setProgress(Integer.parseInt(status.getProgress()));
79             descriptor.setNssiId(status.getResourceInstanceID());
80         }
81         statusResponse.setResponseDescriptor(descriptor);
82         return restUtil.createResponse(200, marshal(statusResponse));
83     }
84
85     @Override
86     protected RestResponse sendRequest(String content) {
87         return sendInternalRequest(content);
88     }
89
90     @Override
91     protected void afterRequest() {
92         //
93     }
94
95     @Override
96     protected void afterQueryJobStatus(ResourceOperationStatus status) {
97         // internal
98     }
99
100     // internal
101     private RestResponse sendInternalRequest(String content) {
102         Header header = new BasicHeader("Authorization", adapterConfig.getInfraAuth());
103         this.nssmfUrl = adapterConfig.getInfraEndpoint() + this.nssmfUrl;
104         return restUtil.send(this.nssmfUrl, this.httpMethod, content, header);
105     }
106
107     @Override
108     protected String getApiVersion() {
109         return NssmfAdapterConsts.CURRENT_INTERNAL_NSSMF_API_VERSION;
110     }
111
112
113     @Override
114     protected SelectionType doQueryNSSISelectionCapability() {
115         return SelectionType.NSSMF;
116     }
117
118     @Override
119     protected String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
120         return doWrapModifyReqBody(nbiRequest);
121     }
122
123     protected abstract String doWrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
124
125     @Override
126     protected RestResponse doQuerySubnetCapability(QuerySubnetCapability req) throws ApplicationException {
127         // handler
128         return sendRequest(marshal(req));
129     }
130 }