2 * Copyright 2018 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.aai.Pnf;
22 import org.onap.aai.esr.entity.aai.PnfList;
23 import org.onap.aai.esr.entity.rest.PnfRegisterInfo;
24 import org.onap.aai.esr.exception.ExceptionUtil;
25 import org.onap.aai.esr.exception.ExtsysException;
26 import org.onap.aai.esr.externalservice.aai.NetworkProxy;
27 import org.onap.aai.esr.util.PnfManagerUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import com.google.gson.Gson;
32 public class PnfManagerWrapper {
33 private static PnfManagerWrapper pnfManagerWrapper;
34 private static final Logger LOG = LoggerFactory.getLogger(PnfManagerWrapper.class);
36 private static PnfManagerUtil pnfManagerUtil = new PnfManagerUtil();
37 private static NetworkProxy networkProxy = new NetworkProxy();
40 * get PnfManagerWrapper instance.
42 * @return pnf manager wrapper instance
44 public static PnfManagerWrapper getInstance() {
45 if (pnfManagerWrapper == null) {
46 pnfManagerWrapper = new PnfManagerWrapper(networkProxy);
48 return pnfManagerWrapper;
51 public PnfManagerWrapper(NetworkProxy networkProxy){
52 PnfManagerWrapper.networkProxy = networkProxy;
58 public Response queryPnfList() {
59 List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
60 PnfList pnfList = new PnfList();
62 String pnflistStr = networkProxy.queryPnfList();
63 pnfList = new Gson().fromJson(pnflistStr, PnfList.class);
64 LOG.info("Response from AAI by query PNF list: " + pnflistStr);
65 esrPnfList = getEsrPnfList(pnfList);
66 return Response.ok(esrPnfList).build();
67 } catch (ExtsysException e) {
68 LOG.error("Query VNFM list failed !", e);
69 return Response.ok(esrPnfList).build();
77 private List<PnfRegisterInfo> getEsrPnfList(PnfList pnfList) {
78 List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
79 for (int i = 0; i < pnfList.getPnf().size(); i++) {
80 Pnf pnf = pnfList.getPnf().get(i);
81 PnfRegisterInfo pnfRegisterInfo = pnfManagerUtil.pnf2PnfRegisterInfo(pnf);
82 esrPnfList.add(pnfRegisterInfo);
91 public Response queryPnfById(String pnfId) {
92 PnfRegisterInfo pnfRegisterInfo = queryPnf(pnfId);
93 return Response.ok(pnfRegisterInfo).build();
96 private PnfRegisterInfo queryPnf(String pnfId) {
98 PnfRegisterInfo pnfRegisterInfo = new PnfRegisterInfo();
100 String pnfStr = networkProxy.queryPNF(pnfId);
101 LOG.info("Response from AAI by query PNF: " + pnfStr);
102 pnf = new Gson().fromJson(pnfStr, Pnf.class);
103 pnfRegisterInfo = pnfManagerUtil.pnf2PnfRegisterInfo(pnf);
104 } catch (ExtsysException e) {
105 LOG.error("Query PNF detail failed! PNF ID: " + pnfId, e);
107 return pnfRegisterInfo;
114 public Response delPnf(String pnfId) {
115 String resourceVersion = getResourceVersion(pnfId);
117 networkProxy.deletePnf(pnfId, resourceVersion);
118 return Response.noContent().build();
119 } catch (ExtsysException e) {
120 LOG.error("Delete PNF from A&AI failed! PNF ID: " + pnfId + "resouce-version:" + resourceVersion, e);
121 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
129 private String getResourceVersion(String pnfId) {
130 String resourceVersion = null;
132 String pnfStr = networkProxy.queryPNF(pnfId);
133 Pnf pnf = new Gson().fromJson(pnfStr, Pnf.class);
134 if (pnf != null && pnf.getResourceVersion() != null) {
135 resourceVersion = pnf.getResourceVersion();
137 } catch (ExtsysException e) {
138 LOG.error("Query PNF detail failed! PNF ID: " + pnfId, e);
140 return resourceVersion;
148 public Response updatePnf(PnfRegisterInfo pnfRegisterInfo, String pnfId) {
149 String resourceVersion = getResourceVersion(pnfId);
150 Pnf pnf = pnfManagerUtil.pnfRegisterInfo2pnf(pnfRegisterInfo);
151 pnf.setResourceVersion(resourceVersion);
153 networkProxy.registerPnf(pnfId, pnf);
154 return Response.ok().build();
155 } catch (ExtsysException e) {
156 LOG.error("Update PNF failed !", e);
157 throw ExceptionUtil.buildExceptionResponse(e.getMessage());
165 public Response registerPnf(PnfRegisterInfo pnfRegisterInfo) {
166 Pnf pnf = pnfManagerUtil.pnfRegisterInfo2pnf(pnfRegisterInfo);
167 String pnfName = pnf.getPnfName();
169 networkProxy.registerPnf(pnfName, pnf);
170 return Response.ok().build();
171 } catch (ExtsysException e) {
172 LOG.error("Register PNF failed !", e);
173 throw ExceptionUtil.buildExceptionResponse(e.getMessage());