0683769fe0b8214ba52bda4473bd44bda902bd9f
[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.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;
31
32 public class PnfManagerWrapper {
33     private static PnfManagerWrapper pnfManagerWrapper;
34     private static final Logger LOG = LoggerFactory.getLogger(PnfManagerWrapper.class);
35
36     private static PnfManagerUtil pnfManagerUtil = new PnfManagerUtil();
37     private static NetworkProxy networkProxy = new NetworkProxy();
38
39     /**
40      * get PnfManagerWrapper instance.
41      * 
42      * @return pnf manager wrapper instance
43      */
44     public static PnfManagerWrapper getInstance() {
45         if (pnfManagerWrapper == null) {
46             pnfManagerWrapper = new PnfManagerWrapper(networkProxy);
47         }
48         return pnfManagerWrapper;
49     }
50     
51     public PnfManagerWrapper(NetworkProxy networkProxy){
52         PnfManagerWrapper.networkProxy = networkProxy;
53     }
54
55     /**
56      * @return
57      */
58     public Response queryPnfList() {
59         List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
60         PnfList pnfList = new PnfList();
61         try {
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();
70         }
71     }
72
73     /**
74      * @param pnfList
75      * @return
76      */
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);
83         }
84         return esrPnfList;
85     }
86
87     /**
88      * @param pnfId
89      * @return
90      */
91     public Response queryPnfById(String pnfId) {
92         PnfRegisterInfo pnfRegisterInfo = queryPnf(pnfId);
93         return Response.ok(pnfRegisterInfo).build();
94     }
95     
96     private PnfRegisterInfo queryPnf(String pnfId) {
97         Pnf pnf = new Pnf();
98         PnfRegisterInfo pnfRegisterInfo = new PnfRegisterInfo();
99         try {
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);
106         }
107         return pnfRegisterInfo;
108     }
109
110     /**
111      * @param pnfId
112      * @return
113      */
114     public Response delPnf(String pnfId) {
115         String resourceVersion = getResourceVersion(pnfId);
116         try {
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());
122         }
123     }
124
125     /**
126      * @param pnfId
127      * @return
128      */
129     private String getResourceVersion(String pnfId) {
130         String resourceVersion = null;
131         try {
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();
136             }
137         } catch (ExtsysException e) {
138             LOG.error("Query PNF detail failed! PNF ID: " + pnfId, e);
139         }
140         return resourceVersion;
141     }
142
143     /**
144      * @param pnf
145      * @param pnfId
146      * @return
147      */
148     public Response updatePnf(PnfRegisterInfo pnf, String pnfId) {
149         // TODO Auto-generated method stub
150         return null;
151     }
152
153     /**
154      * @param pnf
155      * @return
156      */
157     public Response registerPnf(PnfRegisterInfo pnfRegisterInfo) {
158         Pnf pnf = pnfManagerUtil.pnfRegisterInfo2pnf(pnfRegisterInfo);
159         String pnfName = pnf.getPnfName();
160         try {
161             networkProxy.registerPnf(pnfName, pnf);
162             return Response.ok().build();
163         } catch (ExtsysException e) {
164             LOG.error("Register PNF failed !", e);
165             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
166         }
167     }
168 }