Upversion to oparent 2.0.0
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / VimManagerWrapper.java
1 /**
2  * Copyright 2017-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.CloudRegionDetail;
22 import org.onap.aai.esr.entity.aai.CloudRegionList;
23 import org.onap.aai.esr.entity.aai.Complex;
24 import org.onap.aai.esr.entity.aai.ComplexList;
25 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
26 import org.onap.aai.esr.entity.aai.Relationship;
27 import org.onap.aai.esr.entity.aai.RelationshipData;
28 import org.onap.aai.esr.entity.rest.VimRegisterInfo;
29 import org.onap.aai.esr.entity.rest.VimRegisterResponse;
30 import org.onap.aai.esr.exception.ExceptionUtil;
31 import org.onap.aai.esr.exception.ExtsysException;
32 import org.onap.aai.esr.externalservice.aai.CloudRegionProxy;
33 import org.onap.aai.esr.externalservice.cloud.Tenant;
34 import org.onap.aai.esr.externalservice.cloud.VimManagerProxy;
35 import org.onap.aai.esr.util.ExtsysUtil;
36 import org.onap.aai.esr.util.VimManagerUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import com.google.gson.Gson;
40
41 public class VimManagerWrapper {
42
43     private static VimManagerWrapper vimManagerWrapper;
44     private static final Logger LOG = LoggerFactory.getLogger(VimManagerWrapper.class);
45
46     private static VimManagerUtil vimManagerUtil = new VimManagerUtil();
47
48     private static CloudRegionProxy cloudRegionProxy = new CloudRegionProxy();
49     
50     private static VimManagerProxy vimManagerProxy = new VimManagerProxy();
51
52     private static ExtsysUtil extsysUtil = new ExtsysUtil();
53
54     /**
55      * get VimManagerWrapper instance.
56      * 
57      * @return vim manager wrapper instance
58      */
59     public static VimManagerWrapper getInstance() {
60         if (vimManagerWrapper == null) {
61             vimManagerWrapper = new VimManagerWrapper(cloudRegionProxy, vimManagerProxy);
62         }
63         return vimManagerWrapper;
64     }
65     
66     public VimManagerWrapper(CloudRegionProxy cloudRegionProxy, VimManagerProxy vimManagerProxy) {
67         VimManagerWrapper.cloudRegionProxy = cloudRegionProxy;
68         VimManagerWrapper.vimManagerProxy = vimManagerProxy;
69     }
70
71     public Response registerVim(VimRegisterInfo vimRegisterInfo) {
72         LOG.info("Start register VIM, input VIM info is: " + extsysUtil.objectToString(vimRegisterInfo));
73         VimRegisterResponse result = new VimRegisterResponse();
74         CloudRegionDetail cloudRegion = vimManagerUtil.vimRegisterInfo2CloudRegion(vimRegisterInfo);
75         String cloudOwner = vimRegisterInfo.getCloudOwner();
76         String cloudRegionId = vimRegisterInfo.getCloudRegionId();
77         String physicalLocationId = vimRegisterInfo.getPhysicalLocationId();
78         String complexName = getComplexName(physicalLocationId);
79         cloudRegion.setComplexName(complexName);
80         try {
81             cloudRegionProxy.registerVim(cloudOwner, cloudRegionId, cloudRegion);
82             result.setCloudOwner(cloudOwner);
83             result.setCloudRegionId(cloudRegionId);
84             createRelationship(cloudOwner, cloudRegionId, physicalLocationId);
85             updateVimWithMultiCloud(cloudRegion, cloudOwner, cloudRegionId);
86             return Response.ok(result).build();
87         } catch (ExtsysException error) {
88             LOG.error("Register VIM failed !", error);
89             throw ExceptionUtil.buildExceptionResponse(error.getMessage());
90         }
91     }
92
93     private void updateVimWithMultiCloud(CloudRegionDetail cloudRegion, String cloudOwner, String cloudRegionId) {
94         Tenant tenant = new Tenant();
95         tenant.setDefaultTenant(cloudRegion.getEsrSystemInfoList().getEsrSystemInfo().get(0).getDefaultTenant());
96         try {
97             vimManagerProxy.updateVim(cloudOwner, cloudRegionId, tenant);
98         } catch (ExtsysException e) {
99             LOG.error("Update VIM by Multi-cloud failed !", e);
100         }
101     }
102     
103     private void createRelationship(String cloudOwner, String cloudRegionId, String physicalLocationId) {
104         Relationship relationship = new Relationship();
105         RelationshipData relationshipData = new RelationshipData();
106         List<RelationshipData> relationshipDatas = new ArrayList<>();
107         String relatedLink = "/aai/v11/cloud-infrastructure/complexes/complex/" + physicalLocationId;
108         relationship.setRelatedTo("complex");
109         relationship.setRelatedLink(relatedLink);
110         relationshipData.setRelationshipKey("complex.physical-location-id");
111         relationshipData.setRelationshipValue(physicalLocationId);
112         relationshipDatas.add(relationshipData);
113         relationship.setRelationshipData(relationshipDatas);
114         try {
115             cloudRegionProxy.createCloudRegionRelationShip(cloudOwner, cloudRegionId, relationship);
116         } catch (ExtsysException e) {
117             LOG.error("Create relationship between cloudRegion and complex failed !", e);
118         }
119     }
120
121     public Response updateVim(String cloudOwner, String cloudRegionId, VimRegisterInfo vimRegisterInfo) {
122         LOG.info("Start update VIM, input VIM info is: " + extsysUtil.objectToString(vimRegisterInfo));
123         VimRegisterResponse result = new VimRegisterResponse();
124
125         CloudRegionDetail cloudRegionDetail = getVimUpdateInfo(vimRegisterInfo);
126         try {
127             cloudRegionProxy.registerVim(cloudOwner, cloudRegionId, cloudRegionDetail);
128             result.setCloudOwner(cloudOwner);
129             result.setCloudRegionId(cloudRegionId);
130             return Response.ok(result).build();
131         } catch (ExtsysException e) {
132             LOG.error("Update VIM failed !", e);
133             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
134         }
135     }
136
137     public Response queryVimListDetails() {
138         List<VimRegisterInfo> vimRegisterInfos = new ArrayList<>();
139         CloudRegionList cloudRegionList = new CloudRegionList();
140         try {
141             String aaiVimList = cloudRegionProxy.qureyVimList();
142             cloudRegionList = new Gson().fromJson(aaiVimList, CloudRegionList.class);
143             vimRegisterInfos = getVimDetailList(cloudRegionList);
144             return Response.ok(vimRegisterInfos).build();
145         } catch (ExtsysException error) {
146             LOG.error("Query vim list details failed !", error);
147             return Response.ok(vimRegisterInfos).build();
148         }
149
150     }
151
152     public Response queryVimById(String cloudOwner, String cloudRegionId) {
153         VimRegisterInfo vim = new VimRegisterInfo();
154         CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
155         try {
156             String cloudRegionstr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
157             LOG.info("Response from AAI by query VIM: " + cloudRegionstr);
158             cloudRegionDetail = new Gson().fromJson(cloudRegionstr, CloudRegionDetail.class);
159             vim = vimManagerUtil.cloudRegion2VimRegisterInfo(cloudRegionDetail);
160             return Response.ok(vim).build();
161         } catch (ExtsysException e) {
162             LOG.error("Query vim details by ID failed !", e);
163             return Response.ok(vim).build();
164         }
165
166     }
167
168     private List<VimRegisterInfo> getVimDetailList(CloudRegionList cloudRegionList) {
169         List<VimRegisterInfo> vimRegisterInfos = new ArrayList<>();
170         int cloudRegionNum = cloudRegionList.getCloudRegion().size();
171         for (int i = 0; i < cloudRegionNum; i++) {
172             String cloudOwner = cloudRegionList.getCloudRegion().get(i).getCloudOwner();
173             String cloudRegionId = cloudRegionList.getCloudRegion().get(i).getCloudRegionId();
174             VimRegisterInfo vimRegisterInfo = getVimDetail(cloudOwner, cloudRegionId);
175             vimRegisterInfos.add(vimRegisterInfo);
176         }
177         return vimRegisterInfos;
178     }
179
180     private VimRegisterInfo getVimDetail(String cloudOwner, String cloudRegionId) {
181         CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
182         VimRegisterInfo registeredVimInfo = new VimRegisterInfo();
183         try {
184             String cloudRegionstr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
185             cloudRegionDetail = new Gson().fromJson(cloudRegionstr, CloudRegionDetail.class);
186             registeredVimInfo = vimManagerUtil.cloudRegion2VimRegisterInfo(cloudRegionDetail);
187         } catch (ExtsysException error) {
188             LOG.error("query VIM detail failed ! cloud-owner = " + cloudOwner + ", cloud-region-id = " + cloudRegionId,
189                     error);
190         }
191         return registeredVimInfo;
192     }
193
194     private CloudRegionDetail getOriginalCloudRegion(String cloudOwner, String cloudRegionId) {
195         CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
196         try {
197             String cloudRegionstr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
198             cloudRegionDetail = new Gson().fromJson(cloudRegionstr, CloudRegionDetail.class);
199             return cloudRegionDetail;
200         } catch (ExtsysException error) {
201             LOG.error("query VIM detail failed ! cloud-owner = " + cloudOwner + ", cloud-region-id = " + cloudRegionId,
202                     error);
203             return null;
204         }
205     }
206
207     private CloudRegionDetail getVimUpdateInfo(VimRegisterInfo vimRegisterInfo) {
208         String cloudOwner = vimRegisterInfo.getCloudOwner();
209         String cloudRegionId = vimRegisterInfo.getCloudRegionId();
210         String resourceVersion = null;
211         CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
212         CloudRegionDetail originalCloudRegionDetail = getOriginalCloudRegion(cloudOwner, cloudRegionId);
213         if (originalCloudRegionDetail != null) {
214             resourceVersion = originalCloudRegionDetail.getResourceVersion();
215             cloudRegionDetail = vimManagerUtil.vimRegisterInfo2CloudRegion(vimRegisterInfo);
216             if (resourceVersion != null) {
217                 cloudRegionDetail.setResourceVersion(resourceVersion);
218                 EsrSystemInfo originalSystemInfo = originalCloudRegionDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
219                 cloudRegionDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
220                 .setEsrSystemInfoId(originalSystemInfo.getEsrSystemInfoId());
221                 cloudRegionDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
222                 .setResouceVersion(originalSystemInfo.getResouceVersion());
223             }
224         }
225         return cloudRegionDetail;
226     }
227
228     public Response delVim(String cloudOwner, String cloudRegionId) {
229         CloudRegionDetail cloudRegionDetail = queryCloudRegionDetail(cloudOwner, cloudRegionId);
230         String resourceVersion = cloudRegionDetail.getResourceVersion();
231         try {
232             cloudRegionProxy.deleteVim(cloudOwner, cloudRegionId, resourceVersion);
233             return Response.noContent().build();
234         } catch (ExtsysException e) {
235             LOG.error("Delete cloud region from A&AI failed! cloud-owner = " + cloudOwner + ", cloud-region-id = "
236                     + cloudRegionId + "resouce-version:" + resourceVersion, e);
237             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
238         }
239     }
240
241     private CloudRegionDetail queryCloudRegionDetail(String cloudOwner, String cloudRegionId) {
242         CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
243         try {
244             String cloudRegionStr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
245             LOG.info("Response from AAI by query cloud region: " + cloudRegionStr);
246             cloudRegionDetail = new Gson().fromJson(cloudRegionStr, CloudRegionDetail.class);
247         } catch (ExtsysException e) {
248             LOG.error("Query EMS detail failed! cloud-owner = " + cloudOwner + ", cloud-region-id = " + cloudRegionId,
249                     e);
250             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
251         }
252         return cloudRegionDetail;
253     }
254     
255     private String getComplexName(String physicalLocationId) {
256         Complex complex = new Complex();
257         try {
258             String complexStr = cloudRegionProxy.queryComplex(physicalLocationId);
259             LOG.info("The complexes query result is: " + complexStr);
260             complex = new Gson().fromJson(complexStr, Complex.class);
261             return complex.getComplexName();
262         } catch (ExtsysException e) {
263             LOG.error("Query complex by physical location Id failed !", e);
264             return null;
265         }
266     }
267
268     public Response queryComplexes() {
269         ComplexList complexList = new ComplexList();
270         List<String> physicalLocationIdList = new ArrayList<>();
271         try {
272             String complexesString = cloudRegionProxy.qureyComplexes();
273             LOG.info("The complexes query result is: " + complexesString);
274             complexList = new Gson().fromJson(complexesString, ComplexList.class);
275             for (int i=0; i<complexList.getComplex().size(); i++) {
276                 physicalLocationIdList.add(complexList.getComplex().get(i).getPhysicalLocationId());
277             }
278         } catch (ExtsysException e) {
279             LOG.error("Query complexes failed !", e);
280         }
281         return Response.ok(physicalLocationIdList).build();
282     }
283 }

© 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