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.rest.EmsRegisterInfo;
24 import org.onap.aai.esr.exception.ExceptionUtil;
25 import org.onap.aai.esr.exception.ExtsysException;
26 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
27 import org.onap.aai.esr.util.EmsManagerUtil;
28 import org.onap.aai.esr.entity.aai.EsrEmsDetail;
29 import org.onap.aai.esr.entity.aai.EsrEmsList;
30 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
31 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.Gson;
37 public class EmsManagerWrapper {
38 private static EmsManagerWrapper emsManagerWrapper;
39 private static final Logger LOG = LoggerFactory.getLogger(EmsManagerWrapper.class);
40 private static EmsManagerUtil emsManagerUtil = new EmsManagerUtil();
43 * get VnfmManagerWrapper instance.
44 * @return ems manager wrapper instance
46 public static EmsManagerWrapper getInstance() {
47 if (emsManagerWrapper == null) {
48 emsManagerWrapper = new EmsManagerWrapper();
50 return emsManagerWrapper;
53 public Response registerEms(EmsRegisterInfo emsRegisterInfo) {
54 CommonRegisterResponse result = new CommonRegisterResponse();
55 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
56 esrEmsDetail = emsManagerUtil.emsRegisterInfo2EsrEms(emsRegisterInfo);
57 String emsId = esrEmsDetail.getEmsId();
59 ExternalSystemProxy.registerEms(emsId, esrEmsDetail);
61 return Response.ok(result).build();
62 } catch (ExtsysException e) {
63 LOG.error("Register EMS failed !", e);
64 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
68 public Response updateEms(EmsRegisterInfo emsRegisterInfo, String emsId) {
69 CommonRegisterResponse result = new CommonRegisterResponse();
70 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
71 esrEmsDetail = getNewEsrEmsDetail(emsRegisterInfo, emsId);
73 ExternalSystemProxy.registerEms(emsId, esrEmsDetail);
75 return Response.ok(result).build();
76 } catch (ExtsysException e) {
77 LOG.error("Update VNFM failed !", e);
78 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
82 public Response queryEmsList() {
83 List<EmsRegisterInfo> emsList = new ArrayList<>();
84 EsrEmsList esrEms = new EsrEmsList();
86 String esrEmsStr = ExternalSystemProxy.queryEmsList();
87 esrEms = new Gson().fromJson(esrEmsStr, EsrEmsList.class);
88 LOG.info("Response from AAI by query EMS list: " + esrEms);
89 emsList = getEmsDetailList(esrEms);
90 return Response.ok(emsList).build();
91 } catch (ExtsysException e) {
92 LOG.error("Query EMS list failed !", e);
93 return Response.ok(emsList).build();
97 public Response queryEmsById(String emsId) {
99 ems = queryEmsDetail(emsId);
101 return Response.ok(ems).build();
103 return Response.ok(ems).build();
107 public Response delEms(String emsId) {
108 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
109 esrEmsDetail = queryEsrEmsDetail(emsId);
110 String resourceVersion = esrEmsDetail.getResourceVersion();
112 ExternalSystemProxy.deleteEms(emsId, resourceVersion);
113 return Response.noContent().build();
114 } catch (ExtsysException e) {
116 "Delete EMS from A&AI failed! EMS ID: " + emsId + "resouce-version:" + resourceVersion,
118 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
122 private EmsRegisterInfo queryEmsDetail(String emsId) {
123 EmsRegisterInfo emsRegisterInfo = new EmsRegisterInfo();
124 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
126 String esrEmsStr = ExternalSystemProxy.queryEmsDetail(emsId);
127 LOG.info("Response from AAI by query EMS: " + esrEmsStr);
128 esrEmsDetail = new Gson().fromJson(esrEmsStr, EsrEmsDetail.class);
129 emsRegisterInfo = emsManagerUtil.EsrEms2EmsRegisterInfo(esrEmsDetail);
130 } catch (ExtsysException e) {
131 LOG.error("Query EMS detail failed! EMS ID: " + emsId, e);
133 return emsRegisterInfo;
136 private List<EmsRegisterInfo> getEmsDetailList(EsrEmsList esrEms) {
137 List<EmsRegisterInfo> emsInfoList = new ArrayList<>();
138 EmsRegisterInfo emsInfo = new EmsRegisterInfo();
139 for (int i = 0; i < esrEms.getEsrEms().size(); i++) {
140 String emsId = esrEms.getEsrEms().get(i).getEmsId();
141 emsInfo = queryEmsDetail(emsId);
142 if (emsInfo != null) {
143 emsInfoList.add(emsInfo);
149 private EsrEmsDetail queryEsrEmsDetail (String emsId) {
150 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
152 String esrEmsStr = ExternalSystemProxy.queryEmsDetail(emsId);
153 LOG.info("Response from AAI by query EMS: " + esrEmsStr);
154 esrEmsDetail = new Gson().fromJson(esrEmsStr, EsrEmsDetail.class);
155 } catch (ExtsysException e) {
156 LOG.error("Query EMS detail failed! EMS ID: " + emsId, e);
161 private EsrEmsDetail getNewEsrEmsDetail(EmsRegisterInfo emsRegisterInfo, String emsId) {
162 EsrEmsDetail esrEmsDetail = new EsrEmsDetail();
163 List<EsrSystemInfo> newEsrSysInfoList = new ArrayList<>();
164 EsrSystemInfo newEsrSystemInfo = new EsrSystemInfo();
165 EsrEmsDetail oriEsrEmsDetail = new EsrEmsDetail();
166 List<EsrSystemInfo> oriEsrSysInfoList = new ArrayList<>();
167 EsrSystemInfo originalEsrSystemInfo = new EsrSystemInfo();
169 oriEsrEmsDetail = queryEsrEmsDetail(emsId);
170 esrEmsDetail = emsManagerUtil.emsRegisterInfo2EsrEms(emsRegisterInfo);
171 String emsResourceVersion = oriEsrEmsDetail.getResourceVersion();
172 esrEmsDetail.setResourceVersion(emsResourceVersion);
173 esrEmsDetail.setEmsId(emsId);
174 newEsrSysInfoList = esrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo();
175 oriEsrSysInfoList = oriEsrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo();
176 for (int i = 0; i < oriEsrSysInfoList.size(); i++) {
177 originalEsrSystemInfo = oriEsrSysInfoList.get(i);
178 for (int j = 0; j < newEsrSysInfoList.size(); j++) {
179 newEsrSystemInfo = newEsrSysInfoList.get(j);
180 if (originalEsrSystemInfo.getSystemType().equals(newEsrSystemInfo.getSystemType())) {
181 esrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo().get(j)
182 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
183 esrEmsDetail.getEsrSystemInfoList().getEsrSystemInfo().get(j)
184 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());