Upversion to oparent 2.0.0
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / VnfmManagerWrapper.java
1 /**
2  * Copyright 2017 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.EsrSystemInfo;
22 import org.onap.aai.esr.entity.aai.EsrVnfmDetail;
23 import org.onap.aai.esr.entity.aai.EsrVnfmList;
24 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
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.ExternalSystemProxy;
29 import org.onap.aai.esr.util.VnfmManagerUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
33
34 public class VnfmManagerWrapper {
35     private static VnfmManagerWrapper vnfmManagerWrapper;
36     private static final Logger LOG = LoggerFactory.getLogger(VnfmManagerWrapper.class);
37
38     private static VnfmManagerUtil vnfmManagerUtil = new VnfmManagerUtil();
39     private static ExternalSystemProxy externalSystemProxy = new ExternalSystemProxy();
40
41     /**
42      * get VnfmManagerWrapper instance.
43      * 
44      * @return vnfm manager wrapper instance
45      */
46     public static VnfmManagerWrapper getInstance() {
47         if (vnfmManagerWrapper == null) {
48             vnfmManagerWrapper = new VnfmManagerWrapper(externalSystemProxy);
49         }
50         return vnfmManagerWrapper;
51     }
52     
53     public VnfmManagerWrapper(ExternalSystemProxy externalSystemProxy){
54         VnfmManagerWrapper.externalSystemProxy = externalSystemProxy;
55     }
56
57     public Response registerVnfm(VnfmRegisterInfo vnfm) {
58         CommonRegisterResponse result = new CommonRegisterResponse();
59         EsrVnfmDetail esrVnfmDetail = vnfmManagerUtil.vnfmRegisterInfo2EsrVnfm(vnfm);
60         String vnfmId = esrVnfmDetail.getVnfmId();
61         try {
62             externalSystemProxy.registerVnfm(vnfmId, esrVnfmDetail);
63             result.setId(vnfmId);
64             return Response.ok(result).build();
65         } catch (ExtsysException e) {
66             LOG.error("Register VNFM failed !", e);
67             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
68         }
69     }
70
71     public Response updateVnfm(VnfmRegisterInfo vnfm, String vnfmId) {
72         CommonRegisterResponse result = new CommonRegisterResponse();
73         EsrVnfmDetail originalEsrVnfmDetail = queryEsrVnfmDetail(vnfmId);
74         EsrVnfmDetail esrVnfmDetail = vnfmManagerUtil.vnfmRegisterInfo2EsrVnfm(vnfm);
75         String resourceVersion = getResourceVersion(vnfmId);
76         esrVnfmDetail.setResourceVersion(resourceVersion);
77         esrVnfmDetail.setVnfmId(vnfmId);
78         EsrSystemInfo originalEsrSystemInfo = originalEsrVnfmDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
79         esrVnfmDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
80                 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
81         esrVnfmDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
82                 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
83         try {
84             externalSystemProxy.registerVnfm(vnfmId, esrVnfmDetail);
85             result.setId(vnfmId);
86             return Response.ok(result).build();
87         } catch (ExtsysException e) {
88             LOG.error("Update VNFM failed !", e);
89             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
90         }
91     }
92
93     public Response queryVnfmList() {
94         List<VnfmRegisterInfo> vnfmList = new ArrayList<>();
95         EsrVnfmList esrVnfm = new EsrVnfmList();
96         try {
97             String esrVnfmStr = externalSystemProxy.queryVnfmList();
98             esrVnfm = new Gson().fromJson(esrVnfmStr, EsrVnfmList.class);
99             LOG.info("Response from AAI by query VNFM list: " + esrVnfm);
100             vnfmList = getVnfmDetailList(esrVnfm);
101             return Response.ok(vnfmList).build();
102         } catch (ExtsysException e) {
103             LOG.error("Query VNFM list failed !", e);
104             return Response.ok(vnfmList).build();
105         }
106     }
107
108     public Response queryVnfmById(String vnfmId) {
109         VnfmRegisterInfo vnfm = queryVnfmDetail(vnfmId);
110         if (vnfm != null) {
111             return Response.ok(vnfm).build();
112         } else {
113             return Response.ok(vnfm).build();
114         }
115     }
116
117     public Response delVnfm(String vnfmId) {
118         String resourceVersion = getResourceVersion(vnfmId);
119         try {
120             externalSystemProxy.deleteVnfm(vnfmId, resourceVersion);
121             return Response.noContent().build();
122         } catch (ExtsysException e) {
123             LOG.error("Delete VNFM from A&AI failed! VNFM ID: " + vnfmId + "resouce-version:" + resourceVersion, e);
124             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
125         }
126     }
127
128     private VnfmRegisterInfo queryVnfmDetail(String vnfmId) {
129         VnfmRegisterInfo vnfm = new VnfmRegisterInfo();
130         EsrVnfmDetail esrVnfmDetail = new EsrVnfmDetail();
131         try {
132             String esrVnfmstr = externalSystemProxy.queryVnfmDetail(vnfmId);
133             LOG.info("Response from AAI by query VNFM: " + esrVnfmstr);
134             esrVnfmDetail = new Gson().fromJson(esrVnfmstr, EsrVnfmDetail.class);
135             vnfm = vnfmManagerUtil.esrVnfm2VnfmRegisterInfo(esrVnfmDetail);
136         } catch (ExtsysException e) {
137             LOG.error("Query VNFM detail failed! VNFM ID: " + vnfmId, e);
138         }
139         return vnfm;
140     }
141
142     private List<VnfmRegisterInfo> getVnfmDetailList(EsrVnfmList esrVnfm) {
143         List<VnfmRegisterInfo> vnfmInfoList = new ArrayList<>();
144         for (int i = 0; i < esrVnfm.getEsrVnfm().size(); i++) {
145             String vnfmId = esrVnfm.getEsrVnfm().get(i).getVnfmId();
146             VnfmRegisterInfo vnfmInfo = queryVnfmDetail(vnfmId);
147             if (vnfmInfo != null) {
148                 vnfmInfoList.add(vnfmInfo);
149             }
150         }
151         return vnfmInfoList;
152     }
153
154     private String getResourceVersion(String vnfmId) {
155         String resourceVersion = null;
156         EsrVnfmDetail esrVnfmDetail = queryEsrVnfmDetail(vnfmId);
157         if (esrVnfmDetail != null && esrVnfmDetail.getResourceVersion() != null) {
158             resourceVersion = esrVnfmDetail.getResourceVersion();
159         }
160         return resourceVersion;
161     }
162
163     private EsrVnfmDetail queryEsrVnfmDetail(String vnfmId) {
164         EsrVnfmDetail esrVnfmDetail = new EsrVnfmDetail();
165         try {
166             String esrVnfmstr = externalSystemProxy.queryVnfmDetail(vnfmId);
167             LOG.info("Response from AAI by query VNFM: " + esrVnfmstr);
168             esrVnfmDetail = new Gson().fromJson(esrVnfmstr, EsrVnfmDetail.class);
169         } catch (ExtsysException e) {
170             LOG.error("Query VNFM detail failed! VNFM ID: " + vnfmId, e);
171             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
172         }
173         return esrVnfmDetail;
174     }
175 }

© 2017 ONAP. Copyright © The Linux Foundation ®. All Rights Reserved.
The Linux Foundation has registered trademarks and uses trademarks.
For a list of trademarks of The Linux Foundation, please see our Trademark Usage page.
Linux is a registered trademark of Linus Torvalds.
Privacy Policy and Terms of Use