c017ebaca90994c1f42071bce30e8abf1785944f
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / PnfManagerWrapper.java
1 /**
2  * Copyright 2018 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.aai.esr.wrapper;
17
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.EsrVnfmList;
22 import org.onap.aai.esr.entity.aai.Pnf;
23 import org.onap.aai.esr.entity.aai.PnfList;
24 import org.onap.aai.esr.entity.rest.PnfRegisterInfo;
25 import org.onap.aai.esr.entity.rest.VnfmRegisterInfo;
26 import org.onap.aai.esr.exception.ExceptionUtil;
27 import org.onap.aai.esr.exception.ExtsysException;
28 import org.onap.aai.esr.externalservice.aai.NetworkProxy;
29 import org.onap.aai.esr.util.PnfManagerUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
33
34 public class PnfManagerWrapper {
35     private static PnfManagerWrapper pnfManagerWrapper;
36     private static final Logger LOG = LoggerFactory.getLogger(PnfManagerWrapper.class);
37
38     private static PnfManagerUtil pnfManagerUtil = new PnfManagerUtil();
39     private static NetworkProxy networkProxy = new NetworkProxy();
40
41     /**
42      * get PnfManagerWrapper instance.
43      * 
44      * @return pnf manager wrapper instance
45      */
46     public static PnfManagerWrapper getInstance() {
47         if (pnfManagerWrapper == null) {
48             pnfManagerWrapper = new PnfManagerWrapper(networkProxy);
49         }
50         return pnfManagerWrapper;
51     }
52     
53     public PnfManagerWrapper(NetworkProxy networkProxy){
54         PnfManagerWrapper.networkProxy = networkProxy;
55     }
56
57     /**
58      * @return
59      */
60     public Response queryPnfList() {
61         List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
62         PnfList pnfList = new PnfList();
63         try {
64             String pnflistStr = networkProxy.queryPnfList();
65             pnfList = new Gson().fromJson(pnflistStr, PnfList.class);
66             LOG.info("Response from AAI by query PNF list: " + pnflistStr);
67             esrPnfList = getEsrPnfList(pnfList);
68             return Response.ok(esrPnfList).build();
69         } catch (ExtsysException e) {
70             LOG.error("Query VNFM list failed !", e);
71             return Response.ok(esrPnfList).build();
72         }
73     }
74
75     /**
76      * @param pnfList
77      * @return
78      */
79     private List<PnfRegisterInfo> getEsrPnfList(PnfList pnfList) {
80         List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
81         for (int i = 0; i < pnfList.getPnf().size(); i++) {
82             Pnf pnf = pnfList.getPnf().get(i);
83             PnfRegisterInfo pnfRegisterInfo = pnfManagerUtil.pnf2PnfRegisterInfo(pnf);
84             esrPnfList.add(pnfRegisterInfo);
85         }
86         return esrPnfList;
87     }
88
89     /**
90      * @param pnfId
91      * @return
92      */
93     public Response queryPnfById(String pnfId) {
94         PnfRegisterInfo pnfRegisterInfo = queryPnf(pnfId);
95         return Response.ok(pnfRegisterInfo).build();
96     }
97     
98     private PnfRegisterInfo queryPnf(String pnfId) {
99         Pnf pnf = new Pnf();
100         PnfRegisterInfo pnfRegisterInfo = new PnfRegisterInfo();
101         try {
102             String pnfStr = networkProxy.queryPNF(pnfId);
103             LOG.info("Response from AAI by query PNF: " + pnfStr);
104             pnf = new Gson().fromJson(pnfStr, Pnf.class);
105             pnfRegisterInfo = pnfManagerUtil.pnf2PnfRegisterInfo(pnf);
106         } catch (ExtsysException e) {
107             LOG.error("Query PNF detail failed! PNF ID: " + pnfId, e);
108         }
109         return pnfRegisterInfo;
110     }
111
112     /**
113      * @param pnfId
114      * @return
115      */
116     public Response delPnf(String pnfId) {
117         // TODO Auto-generated method stub
118         return null;
119     }
120
121     /**
122      * @param pnf
123      * @param pnfId
124      * @return
125      */
126     public Response updatePnf(PnfRegisterInfo pnf, String pnfId) {
127         // TODO Auto-generated method stub
128         return null;
129     }
130
131     /**
132      * @param pnf
133      * @return
134      */
135     public Response registerPnf(PnfRegisterInfo pnfRegisterInfo) {
136         Pnf pnf = pnfManagerUtil.pnfRegisterInfo2pnf(pnfRegisterInfo);
137         String pnfName = pnf.getPnfName();
138         try {
139             networkProxy.registerPnf(pnfName, pnf);
140             return Response.ok().build();
141         } catch (ExtsysException e) {
142             LOG.error("Register PNF failed !", e);
143             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
144         }
145     }
146 }