2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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
11 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.nssmf.manager.impl;
23 import org.apache.http.Header;
24 import org.apache.http.message.BasicHeader;
25 import org.onap.aai.domain.yang.ServiceInstance;
26 import org.onap.so.adapters.nssmf.entity.NssmfInfo;
27 import org.onap.so.adapters.nssmf.entity.RestResponse;
28 import org.onap.so.adapters.nssmf.enums.JobStatus;
29 import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
30 import org.onap.so.adapters.nssmf.util.NssmfAdapterUtil;
31 import org.onap.so.beans.nsmf.*;
32 import org.onap.so.db.request.beans.ResourceOperationStatus;
33 import static java.lang.String.valueOf;
34 import static org.onap.so.adapters.nssmf.enums.JobStatus.*;
35 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.StatusDesc.*;
36 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
37 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.unMarshal;
39 public abstract class ExternalNssmfManager extends BaseNssmfManager {
42 protected String wrapAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
43 return doWrapExtAllocateReqBody(nbiRequest);
46 protected abstract String doWrapExtAllocateReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
49 protected String wrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException {
50 return doWrapModifyReqBody(nbiRequest);
53 protected abstract String doWrapModifyReqBody(NssmfAdapterNBIRequest nbiRequest) throws ApplicationException;
56 protected String wrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException {
57 return doWrapDeAllocateReqBody(deAllocateNssi);
60 protected abstract String doWrapDeAllocateReqBody(DeAllocateNssi deAllocateNssi) throws ApplicationException;
63 protected void afterQueryJobStatus(ResourceOperationStatus status) {
64 if (Integer.parseInt(status.getProgress()) == 100) {
66 ServiceInstance nssiInstance = restUtil.getServiceInstance(serviceInfo);
67 if (nssiInstance == null) {
68 nssiInstance = new ServiceInstance();
71 nssiInstance.setServiceInstanceId(serviceInfo.getNssiId());
72 nssiInstance.setServiceInstanceName(serviceInfo.getNssiName());
73 nssiInstance.setServiceType(serviceInfo.getSST());
75 nssiInstance.setOrchestrationStatus(initStatus);
76 nssiInstance.setModelInvariantId(serviceInfo.getServiceInvariantUuid());
77 nssiInstance.setModelVersionId(serviceInfo.getServiceUuid());
78 nssiInstance.setServiceInstanceLocationId(serviceInfo.getPLMNIdList());
79 nssiInstance.setEnvironmentContext(esrInfo.getNetworkType().getNetworkType());
80 nssiInstance.setServiceRole("nssi");
82 restUtil.createServiceInstance(nssiInstance, serviceInfo);
89 protected String wrapActDeActReqBody(ActDeActNssi actDeActNssi) throws ApplicationException {
90 return marshal(actDeActNssi);
93 protected RestResponse doQueryJobStatus(ResourceOperationStatus status) throws ApplicationException {
94 return doResponseStatus(status);
97 private RestResponse doResponseStatus(ResourceOperationStatus status) throws ApplicationException {
98 RestResponse restResponse = sendRequest(null);
99 JobStatusResponse jobStatusResponse = unMarshal(restResponse.getResponseContent(), JobStatusResponse.class);
101 ResponseDescriptor rspDesc = jobStatusResponse.getResponseDescriptor();
102 rspDesc.setNssiId(status.getResourceInstanceID());
104 jobStatusResponse.setResponseDescriptor(rspDesc);
105 restResponse.setResponseContent(marshal(jobStatusResponse));
106 updateRequestDbJobStatus(rspDesc, status, restResponse);
111 protected String wrapReqBody(Object object) throws ApplicationException {
112 return marshal(object);
116 protected RestResponse sendRequest(String content) throws ApplicationException {
117 return sendExternalRequest(content);
121 protected String getApiVersion() {
127 protected RestResponse sendExternalRequest(String content) throws ApplicationException {
128 NssmfInfo nssmfInfo = restUtil.getNssmfHost(esrInfo);
129 Header header = new BasicHeader("X-Auth-Token", restUtil.getToken(nssmfInfo));
130 String nssmfUrl = nssmfInfo.getUrl() + this.nssmfUrl;
131 return restUtil.send(nssmfUrl, this.httpMethod, content, header);
134 protected void updateRequestDbJobStatus(ResponseDescriptor rspDesc, ResourceOperationStatus status,
135 RestResponse rsp) throws ApplicationException {
137 switch (fromString(rspDesc.getStatus())) {
139 updateDbStatus(status, rsp.getStatus(), STARTED, QUERY_JOB_STATUS_SUCCESS);
142 updateDbStatus(status, rsp.getStatus(), PROCESSING, QUERY_JOB_STATUS_SUCCESS);
145 if (rspDesc.getProgress() == 100) {
146 updateDbStatus(status, rsp.getStatus(), FINISHED, QUERY_JOB_STATUS_SUCCESS);
150 updateDbStatus(status, rsp.getStatus(), ERROR, QUERY_JOB_STATUS_FAILED);
151 throw new ApplicationException(500, QUERY_JOB_STATUS_FAILED);
155 protected void updateDbStatus(ResourceOperationStatus status, int rspStatus, JobStatus jobStatus,
156 String description) {
157 status.setErrorCode(valueOf(rspStatus));
158 status.setStatus(jobStatus.toString());
159 status.setStatusDescription(description);
160 repository.save(status);
164 protected <T> RestResponse doQuerySubnetCapability(T req) throws ApplicationException {
165 RestResponse response = new RestResponse();
166 response.setStatus(200);
167 response.setResponseContent(null);
172 * after request, if response code is 2XX, continue handle, else return
175 protected void afterRequest() throws ApplicationException {
176 if (valueOf(restResponse.getStatus()).startsWith("2")) {
182 protected void doAfterRequest() throws ApplicationException {
184 NssiResponse response = unMarshal(restResponse.getResponseContent(), NssiResponse.class);
185 ResourceOperationStatus status =
186 new ResourceOperationStatus(serviceInfo.getNsiId(), response.getJobId(), serviceInfo.getServiceUuid());
187 status.setResourceInstanceID(response.getNssiId());
189 updateDbStatus(status, restResponse.getStatus(), STARTED, NssmfAdapterUtil.getStatusDesc(actionType));