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;
20 import javax.ws.rs.core.Response;
22 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
23 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
24 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
25 import org.onap.aai.esr.entity.aai.EsrVnfmDetail;
26 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
27 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
28 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
29 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
30 import org.onap.aai.esr.util.VnfmManagerUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.gson.Gson;
36 public class ThirdpatySdncWrapper {
38 private static ThirdpatySdncWrapper thirdpatySdncWrapper;
39 private static final Logger LOG = LoggerFactory.getLogger(ThirdpatySdncWrapper.class);
42 * get ThirdpatySdncWrapper instance.
43 * @return ThirdpatySdnc manager wrapper instance
45 public static ThirdpatySdncWrapper getInstance() {
46 if (thirdpatySdncWrapper == null) {
47 thirdpatySdncWrapper = new ThirdpatySdncWrapper();
49 return thirdpatySdncWrapper;
52 public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
53 CommonRegisterResponse result = new CommonRegisterResponse();
54 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
55 esrSdncDetail = ThirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
56 String sdncId = esrSdncDetail.getThirdpartySdncId();
58 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
60 return Response.ok(result).build();
61 } catch (Exception e) {
63 LOG.error("Register thirdParty SDNC failed !" + e.getMessage());
64 return Response.serverError().build();
69 public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
70 CommonRegisterResponse result = new CommonRegisterResponse();
71 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
72 EsrThirdpartySdncDetail originalEsrSdncDetail = new EsrThirdpartySdncDetail();
73 EsrSystemInfo originalEsrSystemInfo = new EsrSystemInfo();
74 originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
75 esrSdncDetail = ThirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
76 String resourceVersion = originalEsrSdncDetail.getResourceVersion();
77 esrSdncDetail.setResourceVersion(resourceVersion);
78 esrSdncDetail.setThirdpartySdncId(sdncId);
79 originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
80 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
81 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
82 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
83 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
85 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
87 return Response.ok(result).build();
88 } catch (Exception e) {
90 LOG.error("Update VNFM failed !" + e.getMessage());
91 return Response.serverError().build();
95 public Response queryThirdpartySdncList() {
96 ArrayList<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
97 EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
99 String esrSdncStr = ExternalSystemProxy.querySdncList();
100 esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
101 LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
102 sdncList = getSdncDetailList(esrSdnc);
103 return Response.ok(sdncList).build();
104 } catch (Exception e) {
106 LOG.error("Query thirdparty SDNC list failed !");
107 return Response.serverError().build();
111 public Response queryThirdpartySdncById(String thirdpartySdncId) {
112 ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
113 thirdpartySdnc = querySdncDetail(thirdpartySdncId);
114 if(thirdpartySdnc != null) {
115 return Response.ok(thirdpartySdnc).build();
117 return Response.ok().build();
121 public Response delThirdpartySdnc(String thirdpartySdncId) {
122 EsrThirdpartySdncDetail thirdpartySdncDetail = new EsrThirdpartySdncDetail();
123 thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
124 String resourceVersion = thirdpartySdncDetail.getResourceVersion();
125 if (resourceVersion != null) {
127 ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
128 return Response.ok().build();
129 } catch (Exception e) {
131 LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId + "resouce-version:"
132 + resourceVersion, e.getMessage());
133 return Response.noContent().build();
136 LOG.error("resouce-version is null ! Can not delete resouce from A&AI. ");
137 return Response.serverError().build();
141 private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
142 ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
143 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
145 String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
146 LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
147 esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
148 sdncRegisterInfo = ThirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
149 return sdncRegisterInfo;
150 } catch (Exception e) {
152 LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
157 private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
158 ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
159 ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
160 for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
161 String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
162 sdncInfo = querySdncDetail(sdncId);
163 if (sdncInfo != null) {
164 sdncInfoList.add(sdncInfo);
170 private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
171 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
173 String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
174 LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
175 esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
176 } catch (Exception e) {
178 LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e.getMessage());
180 return esrSdncDetail;