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.rest.EmsRegisterInfo;
22 import org.onap.aai.esr.exception.ExceptionUtil;
23 import org.onap.aai.esr.exception.ExtsysException;
24 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
25 import org.onap.aai.esr.util.EmsManagerUtil;
26 import org.onap.aai.esr.entity.aai.EsrEmsDetail;
27 import org.onap.aai.esr.entity.aai.EsrEmsList;
28 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
29 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
34 public class EmsManagerWrapper {
35 private static EmsManagerWrapper emsManagerWrapper;
36 private static final Logger LOG = LoggerFactory.getLogger(EmsManagerWrapper.class);
37 private static EmsManagerUtil emsManagerUtil = new EmsManagerUtil();
40 * get VnfmManagerWrapper instance.
42 * @return ems manager wrapper instance
44 public static EmsManagerWrapper getInstance() {
45 if (emsManagerWrapper == null) {
46 emsManagerWrapper = new EmsManagerWrapper();
48 return emsManagerWrapper;
51 public Response registerEms(EmsRegisterInfo emsRegisterInfo) {
52 CommonRegisterResponse result = new CommonRegisterResponse();
53 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
54 esrEmsDetail = emsManagerUtil.emsRegisterInfo2EsrEms(emsRegisterInfo);
55 String emsId = esrEmsDetail.getEmsId();
57 ExternalSystemProxy.registerEms(emsId, esrEmsDetail);
59 return Response.ok(result).build();
60 } catch (ExtsysException e) {
61 LOG.error("Register EMS failed !", e);
62 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
66 public Response updateEms(EmsRegisterInfo emsRegisterInfo, String emsId) {
67 CommonRegisterResponse result = new CommonRegisterResponse();
68 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
69 esrEmsDetail = getNewEsrEmsDetail(emsRegisterInfo, emsId);
71 ExternalSystemProxy.registerEms(emsId, esrEmsDetail);
73 return Response.ok(result).build();
74 } catch (ExtsysException e) {
75 LOG.error("Update VNFM failed !", e);
76 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
80 public Response queryEmsList() {
81 List<EmsRegisterInfo> emsList = new ArrayList<>();
82 EsrEmsList esrEms = new EsrEmsList();
84 String esrEmsStr = ExternalSystemProxy.queryEmsList();
85 esrEms = new Gson().fromJson(esrEmsStr, EsrEmsList.class);
86 LOG.info("Response from AAI by query EMS list: " + esrEms);
87 emsList = getEmsDetailList(esrEms);
88 return Response.ok(emsList).build();
89 } catch (ExtsysException e) {
90 LOG.error("Query EMS list failed !", e);
91 return Response.ok(emsList).build();
95 public Response queryEmsById(String emsId) {
97 ems = queryEmsDetail(emsId);
99 return Response.ok(ems).build();
101 return Response.ok(ems).build();
105 public Response delEms(String emsId) {
106 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
107 esrEmsDetail = queryEsrEmsDetail(emsId);
108 String resourceVersion = esrEmsDetail.getResourceVersion();
110 ExternalSystemProxy.deleteEms(emsId, resourceVersion);
111 return Response.noContent().build();
112 } catch (ExtsysException e) {
113 LOG.error("Delete EMS from A&AI failed! EMS ID: " + emsId + "resouce-version:" + resourceVersion, e);
114 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
118 private EmsRegisterInfo queryEmsDetail(String emsId) {
119 EmsRegisterInfo emsRegisterInfo = new EmsRegisterInfo();
120 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
122 String esrEmsStr = ExternalSystemProxy.queryEmsDetail(emsId);
123 LOG.info("Response from AAI by query EMS: " + esrEmsStr);
124 esrEmsDetail = new Gson().fromJson(esrEmsStr, EsrEmsDetail.class);
125 emsRegisterInfo = emsManagerUtil.EsrEms2EmsRegisterInfo(esrEmsDetail);
126 } catch (ExtsysException e) {
127 LOG.error("Query EMS detail failed! EMS ID: " + emsId, e);
129 return emsRegisterInfo;
132 private List<EmsRegisterInfo> getEmsDetailList(EsrEmsList esrEms) {
133 List<EmsRegisterInfo> emsInfoList = new ArrayList<>();
134 EmsRegisterInfo emsInfo = new EmsRegisterInfo();
135 for (int i = 0; i < esrEms.getEsrEms().size(); i++) {
136 String emsId = esrEms.getEsrEms().get(i).getEmsId();
137 emsInfo = queryEmsDetail(emsId);
138 if (emsInfo != null) {
139 emsInfoList.add(emsInfo);
145 private EsrEmsDetail queryEsrEmsDetail(String emsId) {
146 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
148 String esrEmsStr = ExternalSystemProxy.queryEmsDetail(emsId);
149 LOG.info("Response from AAI by query EMS: " + esrEmsStr);
150 esrEmsDetail = new Gson().fromJson(esrEmsStr, EsrEmsDetail.class);
151 } catch (ExtsysException e) {
152 LOG.error("Query EMS detail failed! EMS ID: " + emsId, e);
157 private EsrEmsDetail getNewEsrEmsDetail(EmsRegisterInfo emsRegisterInfo, String emsId) {
158 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
159 List<EsrSystemInfo> newEsrSysInfoList = new ArrayList<>();
160 EsrSystemInfo newEsrSystemInfo = new EsrSystemInfo();
161 EsrEmsDetail oriEsrEmsDetail = new EsrEmsDetail();
162 List<EsrSystemInfo> oriEsrSysInfoList = new ArrayList<>();
163 EsrSystemInfo originalEsrSystemInfo = new EsrSystemInfo();
165 oriEsrEmsDetail = queryEsrEmsDetail(emsId);
166 esrEmsDetail = emsManagerUtil.emsRegisterInfo2EsrEms(emsRegisterInfo);
167 String emsResourceVersion = oriEsrEmsDetail.getResourceVersion();
168 esrEmsDetail.setResourceVersion(emsResourceVersion);
169 esrEmsDetail.setEmsId(emsId);
170 newEsrSysInfoList = esrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo();
171 oriEsrSysInfoList = oriEsrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo();
172 for (int i = 0; i < oriEsrSysInfoList.size(); i++) {
173 originalEsrSystemInfo = oriEsrSysInfoList.get(i);
174 for (int j = 0; j < newEsrSysInfoList.size(); j++) {
175 newEsrSystemInfo = newEsrSysInfoList.get(j);
176 if (originalEsrSystemInfo.getSystemType().equals(newEsrSystemInfo.getSystemType())) {
177 esrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo().get(j)
178 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
179 esrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo().get(j)
180 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());