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);
40 private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();
43 * get ThirdpatySdncWrapper instance.
44 * @return ThirdpatySdnc manager wrapper instance
46 public static ThirdpatySdncWrapper getInstance() {
47 if (thirdpatySdncWrapper == null) {
48 thirdpatySdncWrapper = new ThirdpatySdncWrapper();
50 return thirdpatySdncWrapper;
53 public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
54 CommonRegisterResponse result = new CommonRegisterResponse();
55 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
56 esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
57 String sdncId = esrSdncDetail.getThirdpartySdncId();
59 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
61 return Response.ok(result).build();
62 } catch (Exception e) {
64 LOG.error("Register thirdParty SDNC failed !" + e.getMessage());
65 return Response.serverError().build();
70 public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
71 CommonRegisterResponse result = new CommonRegisterResponse();
72 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
73 EsrThirdpartySdncDetail originalEsrSdncDetail = new EsrThirdpartySdncDetail();
74 EsrSystemInfo originalEsrSystemInfo = new EsrSystemInfo();
75 originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
76 esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
77 String resourceVersion = originalEsrSdncDetail.getResourceVersion();
78 esrSdncDetail.setResourceVersion(resourceVersion);
79 esrSdncDetail.setThirdpartySdncId(sdncId);
80 originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
81 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
82 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
83 esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
84 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
86 ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
88 return Response.ok(result).build();
89 } catch (Exception e) {
91 LOG.error("Update VNFM failed !" + e.getMessage());
92 return Response.serverError().build();
96 public Response queryThirdpartySdncList() {
97 ArrayList<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
98 EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
100 String esrSdncStr = ExternalSystemProxy.querySdncList();
101 esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
102 LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
103 sdncList = getSdncDetailList(esrSdnc);
104 return Response.ok(sdncList).build();
105 } catch (Exception e) {
107 LOG.error("Query thirdparty SDNC list failed !");
108 return Response.ok(sdncList).build();
112 public Response queryThirdpartySdncById(String thirdpartySdncId) {
113 ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
114 thirdpartySdnc = querySdncDetail(thirdpartySdncId);
115 if(thirdpartySdnc != null) {
116 return Response.ok(thirdpartySdnc).build();
118 return Response.ok(thirdpartySdnc).build();
122 public Response delThirdpartySdnc(String thirdpartySdncId) {
123 EsrThirdpartySdncDetail thirdpartySdncDetail = new EsrThirdpartySdncDetail();
124 thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
125 String resourceVersion = thirdpartySdncDetail.getResourceVersion();
126 if (resourceVersion != null) {
128 ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
129 return Response.noContent().build();
130 } catch (Exception e) {
132 LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId + "resouce-version:"
133 + resourceVersion, e.getMessage());
134 return Response.serverError().build();
137 LOG.error("resouce-version is null ! Can not delete resouce from A&AI. ");
138 return Response.serverError().build();
142 private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
143 ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
144 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
146 String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
147 LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
148 esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
149 sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
150 return sdncRegisterInfo;
151 } catch (Exception e) {
153 LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
158 private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
159 ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
160 ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
161 for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
162 String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
163 sdncInfo = querySdncDetail(sdncId);
164 if (sdncInfo != null) {
165 sdncInfoList.add(sdncInfo);
171 private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
172 EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
174 String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
175 LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
176 esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
177 } catch (Exception e) {
179 LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e.getMessage());
181 return esrSdncDetail;