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;
21 import javax.ws.rs.core.Response;
23 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
24 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
25 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
26 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
27 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
28 import org.onap.aai.esr.exception.ExceptionUtil;
29 import org.onap.aai.esr.exception.ExtsysException;
30 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
31 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.Gson;
37 public class ThirdpartySdncWrapper {
39 private static ThirdpartySdncWrapper thirdpatySdncWrapper;
40 private static final Logger LOG = LoggerFactory.getLogger(ThirdpartySdncWrapper.class);
41 private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();
44 * get ThirdpatySdncWrapper instance.
45 * @return ThirdpatySdnc manager wrapper instance
47 public static ThirdpartySdncWrapper getInstance() {
48 if (thirdpatySdncWrapper == null) {
49 thirdpatySdncWrapper = new ThirdpartySdncWrapper();
51 return thirdpatySdncWrapper;
54 public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
55 CommonRegisterResponse result = new CommonRegisterResponse();
56 EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
57 String sdncId = esrSdncDetail.getThirdpartySdncId();
59 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
61 return Response.ok(result).build();
62 } catch (ExtsysException e) {
63 LOG.error("Register thirdParty SDNC failed !" , e);
64 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
69 public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
70 CommonRegisterResponse result = new CommonRegisterResponse();
71 EsrThirdpartySdncDetail originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
72 EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
73 String resourceVersion = originalEsrSdncDetail.getResourceVersion();
74 esrSdncDetail.setResourceVersion(resourceVersion);
75 esrSdncDetail.setThirdpartySdncId(sdncId);
76 EsrSystemInfo originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
77 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
78 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
79 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
80 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
82 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
84 return Response.ok(result).build();
85 } catch (ExtsysException e) {
86 LOG.error("Update VNFM failed !" , e);
87 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
91 public Response queryThirdpartySdncList() {
92 List<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<>();
93 EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
95 String esrSdncStr = ExternalSystemProxy.querySdncList();
96 esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
97 LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
98 sdncList = getSdncDetailList(esrSdnc);
99 } catch (ExtsysException e) {
100 LOG.error("Query thirdparty SDNC list failed !", e);
102 return Response.ok(sdncList).build();
105 public Response queryThirdpartySdncById(String thirdpartySdncId) {
106 ThirdpartySdncRegisterInfo thirdpartySdnc = querySdncDetail(thirdpartySdncId);
107 return Response.ok(thirdpartySdnc).build();
110 public Response delThirdpartySdnc(String thirdpartySdncId) {
111 EsrThirdpartySdncDetail thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
112 String resourceVersion = thirdpartySdncDetail.getResourceVersion();
114 ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
115 return Response.noContent().build();
116 } catch (ExtsysException e) {
117 LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId
118 + "resouce-version:" + resourceVersion, e);
119 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
123 private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
124 ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
125 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
127 String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
128 LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
129 esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
130 sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
131 return sdncRegisterInfo;
132 } catch (ExtsysException e) {
133 LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e);
134 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
138 private List<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
139 List<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<>();
140 for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
141 String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
142 ThirdpartySdncRegisterInfo sdncInfo = querySdncDetail(sdncId);
143 if (sdncInfo != null) {
144 sdncInfoList.add(sdncInfo);
150 private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
151 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
153 String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
154 LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
155 esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
156 } catch (ExtsysException e) {
157 LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e);
158 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
160 return esrSdncDetail;