5efa7434a7480765f714a9f024fb742b22b98e01
[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 javax.ws.rs.core.Response;
19 import org.onap.aai.esr.entity.aai.Pnf;
20 import org.onap.aai.esr.entity.rest.PnfRegisterInfo;
21 import org.onap.aai.esr.exception.ExceptionUtil;
22 import org.onap.aai.esr.exception.ExtsysException;
23 import org.onap.aai.esr.externalservice.aai.NetworkProxy;
24 import org.onap.aai.esr.util.PnfManagerUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import com.google.gson.Gson;
28
29 public class PnfManagerWrapper {
30     private static PnfManagerWrapper pnfManagerWrapper;
31     private static final Logger LOG = LoggerFactory.getLogger(PnfManagerWrapper.class);
32
33     private static PnfManagerUtil pnfManagerUtil = new PnfManagerUtil();
34     private static NetworkProxy networkProxy = new NetworkProxy();
35
36     /**
37      * get PnfManagerWrapper instance.
38      * 
39      * @return pnf manager wrapper instance
40      */
41     public static PnfManagerWrapper getInstance() {
42         if (pnfManagerWrapper == null) {
43             pnfManagerWrapper = new PnfManagerWrapper(networkProxy);
44         }
45         return pnfManagerWrapper;
46     }
47     
48     public PnfManagerWrapper(NetworkProxy networkProxy){
49         PnfManagerWrapper.networkProxy = networkProxy;
50     }
51
52     /**
53      * @return
54      */
55     public Response queryPnfList() {
56         // TODO Auto-generated method stub
57         return null;
58     }
59
60     /**
61      * @param pnfId
62      * @return
63      */
64     public Response queryPnfById(String pnfId) {
65         PnfRegisterInfo pnfRegisterInfo = queryPnf(pnfId);
66         return Response.ok(pnfRegisterInfo).build();
67     }
68     
69     private PnfRegisterInfo queryPnf(String pnfId) {
70         Pnf pnf = new Pnf();
71         PnfRegisterInfo pnfRegisterInfo = new PnfRegisterInfo();
72         try {
73             String pnfStr = networkProxy.queryPNF(pnfId);
74             LOG.info("Response from AAI by query PNF: " + pnfStr);
75             pnf = new Gson().fromJson(pnfStr, Pnf.class);
76             pnfRegisterInfo = pnfManagerUtil.pnf2PnfRegisterInfo(pnf);
77         } catch (ExtsysException e) {
78             LOG.error("Query PNF detail failed! PNF ID: " + pnfId, e);
79         }
80         return pnfRegisterInfo;
81     }
82
83     /**
84      * @param pnfId
85      * @return
86      */
87     public Response delPnf(String pnfId) {
88         // TODO Auto-generated method stub
89         return null;
90     }
91
92     /**
93      * @param pnf
94      * @param pnfId
95      * @return
96      */
97     public Response updatePnf(PnfRegisterInfo pnf, String pnfId) {
98         // TODO Auto-generated method stub
99         return null;
100     }
101
102     /**
103      * @param pnf
104      * @return
105      */
106     public Response registerPnf(PnfRegisterInfo pnfRegisterInfo) {
107         Pnf pnf = pnfManagerUtil.pnfRegisterInfo2pnf(pnfRegisterInfo);
108         String pnfName = pnf.getPnfName();
109         try {
110             networkProxy.registerPnf(pnfName, pnf);
111             return Response.ok().build();
112         } catch (ExtsysException e) {
113             LOG.error("Register PNF failed !", e);
114             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
115         }
116     }
117 }