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