2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.aai.esr.wrapper;
18 import java.util.ArrayList;
19 import java.util.List;
20 import javax.ws.rs.core.Response;
21 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
22 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
23 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
24 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
25 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
26 import org.onap.aai.esr.exception.ExceptionUtil;
27 import org.onap.aai.esr.exception.ExtsysException;
28 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
29 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
34 public class ThirdpartySdncWrapper {
36 private static ThirdpartySdncWrapper thirdpatySdncWrapper;
37 private static final Logger LOG = LoggerFactory.getLogger(ThirdpartySdncWrapper.class);
38 private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();
41 * get ThirdpatySdncWrapper instance.
43 * @return ThirdpatySdnc manager wrapper instance
45 public static ThirdpartySdncWrapper getInstance() {
46 if (thirdpatySdncWrapper == null) {
47 thirdpatySdncWrapper = new ThirdpartySdncWrapper();
49 return thirdpatySdncWrapper;
52 public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
53 CommonRegisterResponse result = new CommonRegisterResponse();
54 EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
55 String sdncId = esrSdncDetail.getThirdpartySdncId();
57 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
59 return Response.ok(result).build();
60 } catch (ExtsysException e) {
61 LOG.error("Register thirdParty SDNC failed !", e);
62 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
67 public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
68 CommonRegisterResponse result = new CommonRegisterResponse();
69 EsrThirdpartySdncDetail originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
70 EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
71 String resourceVersion = originalEsrSdncDetail.getResourceVersion();
72 esrSdncDetail.setResourceVersion(resourceVersion);
73 esrSdncDetail.setThirdpartySdncId(sdncId);
74 EsrSystemInfo originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
75 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
76 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
77 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
78 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
80 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
82 return Response.ok(result).build();
83 } catch (ExtsysException e) {
84 LOG.error("Update VNFM failed !", e);
85 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
89 public Response queryThirdpartySdncList() {
90 List<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<>();
91 EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
93 String esrSdncStr = ExternalSystemProxy.querySdncList();
94 esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
95 LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
96 sdncList = getSdncDetailList(esrSdnc);
97 } catch (ExtsysException e) {
98 LOG.error("Query thirdparty SDNC list failed !", e);
100 return Response.ok(sdncList).build();
103 public Response queryThirdpartySdncById(String thirdpartySdncId) {
104 ThirdpartySdncRegisterInfo thirdpartySdnc = querySdncDetail(thirdpartySdncId);
105 return Response.ok(thirdpartySdnc).build();
108 public Response delThirdpartySdnc(String thirdpartySdncId) {
109 EsrThirdpartySdncDetail thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
110 String resourceVersion = thirdpartySdncDetail.getResourceVersion();
112 ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
113 return Response.noContent().build();
114 } catch (ExtsysException e) {
115 LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId + "resouce-version:"
116 + resourceVersion, e);
117 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
121 private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
122 ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
123 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
125 String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
126 LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
127 esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
128 sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
129 return sdncRegisterInfo;
130 } catch (ExtsysException e) {
131 LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e);
132 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
136 private List<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
137 List<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<>();
138 for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
139 String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
140 ThirdpartySdncRegisterInfo sdncInfo = querySdncDetail(sdncId);
141 if (sdncInfo != null) {
142 sdncInfoList.add(sdncInfo);
148 private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail(String sdncId) {
149 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
151 String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
152 LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
153 esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
154 } catch (ExtsysException e) {
155 LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e);
156 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
158 return esrSdncDetail;