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.EsrVnfmDetail;
25 import org.onap.aai.esr.entity.aai.EsrVnfmList;
26 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
27 import org.onap.aai.esr.entity.rest.VnfmRegisterInfo;
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.VnfmManagerUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.Gson;
37 public class VnfmManagerWrapper {
38 private static VnfmManagerWrapper vnfmManagerWrapper;
39 private static final Logger LOG = LoggerFactory.getLogger(VnfmManagerWrapper.class);
41 private static VnfmManagerUtil vnfmManagerUtil = new VnfmManagerUtil();
44 * get VnfmManagerWrapper instance.
46 * @return vnfm manager wrapper instance
48 public static VnfmManagerWrapper getInstance() {
49 if (vnfmManagerWrapper == null) {
50 vnfmManagerWrapper = new VnfmManagerWrapper();
52 return vnfmManagerWrapper;
55 public Response registerVnfm(VnfmRegisterInfo vnfm) {
56 CommonRegisterResponse result = new CommonRegisterResponse();
57 EsrVnfmDetail esrVnfmDetail = vnfmManagerUtil.vnfmRegisterInfo2EsrVnfm(vnfm);
58 String vnfmId = esrVnfmDetail.getVnfmId();
60 ExternalSystemProxy.registerVnfm(vnfmId, esrVnfmDetail);
62 return Response.ok(result).build();
63 } catch (ExtsysException e) {
64 LOG.error("Register VNFM failed !" , e);
65 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
69 public Response updateVnfm(VnfmRegisterInfo vnfm, String vnfmId) {
70 CommonRegisterResponse result = new CommonRegisterResponse();
71 EsrVnfmDetail originalEsrVnfmDetail = queryEsrVnfmDetail(vnfmId);
72 EsrVnfmDetail esrVnfmDetail = vnfmManagerUtil.vnfmRegisterInfo2EsrVnfm(vnfm);
73 String resourceVersion = getResourceVersion(vnfmId);
74 esrVnfmDetail.setResourceVersion(resourceVersion);
75 esrVnfmDetail.setVnfmId(vnfmId);
76 EsrSystemInfo originalEsrSystemInfo = originalEsrVnfmDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
77 esrVnfmDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
78 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
79 esrVnfmDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
80 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
82 ExternalSystemProxy.registerVnfm(vnfmId, esrVnfmDetail);
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 queryVnfmList() {
92 List<VnfmRegisterInfo> vnfmList = new ArrayList<>();
93 EsrVnfmList esrVnfm = new EsrVnfmList();
95 String esrVnfmStr = ExternalSystemProxy.queryVnfmList();
96 esrVnfm = new Gson().fromJson(esrVnfmStr, EsrVnfmList.class);
97 LOG.info("Response from AAI by query VNFM list: " + esrVnfm);
98 vnfmList = getVnfmDetailList(esrVnfm);
99 return Response.ok(vnfmList).build();
100 } catch (ExtsysException e) {
101 LOG.error("Query VNFM list failed !", e);
102 return Response.ok(vnfmList).build();
106 public Response queryVnfmById(String vnfmId) {
107 VnfmRegisterInfo vnfm = queryVnfmDetail(vnfmId);
109 return Response.ok(vnfm).build();
111 return Response.ok(vnfm).build();
115 public Response delVnfm(String vnfmId) {
116 String resourceVersion = getResourceVersion(vnfmId);
118 ExternalSystemProxy.deleteVnfm(vnfmId, resourceVersion);
119 return Response.noContent().build();
120 } catch (ExtsysException e) {
122 "Delete VNFM from A&AI failed! VNFM ID: " + vnfmId + "resouce-version:" + resourceVersion,
124 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
128 private VnfmRegisterInfo queryVnfmDetail(String vnfmId) {
129 VnfmRegisterInfo vnfm = new VnfmRegisterInfo();
130 EsrVnfmDetail esrVnfmDetail = new EsrVnfmDetail();
132 String esrVnfmstr = ExternalSystemProxy.queryVnfmDetail(vnfmId);
133 LOG.info("Response from AAI by query VNFM: " + esrVnfmstr);
134 esrVnfmDetail = new Gson().fromJson(esrVnfmstr, EsrVnfmDetail.class);
135 vnfm = vnfmManagerUtil.esrVnfm2VnfmRegisterInfo(esrVnfmDetail);
136 } catch (ExtsysException e) {
137 LOG.error("Query VNFM detail failed! VNFM ID: " + vnfmId, e);
142 private List<VnfmRegisterInfo> getVnfmDetailList(EsrVnfmList esrVnfm) {
143 List<VnfmRegisterInfo> vnfmInfoList = new ArrayList<>();
144 for (int i = 0; i < esrVnfm.getEsrVnfm().size(); i++) {
145 String vnfmId = esrVnfm.getEsrVnfm().get(i).getVnfmId();
146 VnfmRegisterInfo vnfmInfo = queryVnfmDetail(vnfmId);
147 if (vnfmInfo != null) {
148 vnfmInfoList.add(vnfmInfo);
154 private String getResourceVersion (String vnfmId) {
155 String resourceVersion = null;
156 EsrVnfmDetail esrVnfmDetail = queryEsrVnfmDetail(vnfmId);
157 if (esrVnfmDetail != null && esrVnfmDetail.getResourceVersion() != null) {
158 resourceVersion = esrVnfmDetail.getResourceVersion();
160 return resourceVersion;
163 private EsrVnfmDetail queryEsrVnfmDetail (String vnfmId) {
164 EsrVnfmDetail esrVnfmDetail = new EsrVnfmDetail();
166 String esrVnfmstr = ExternalSystemProxy.queryVnfmDetail(vnfmId);
167 LOG.info("Response from AAI by query VNFM: " + esrVnfmstr);
168 esrVnfmDetail = new Gson().fromJson(esrVnfmstr, EsrVnfmDetail.class);
169 } catch (ExtsysException e) {
170 LOG.error("Query VNFM detail failed! VNFM ID: " + vnfmId, e);
171 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
173 return esrVnfmDetail;