summaryrefslogtreecommitdiffstats
path: root/esr-mgr/src/main/java/org/onap/aai/esr/wrapper/VimManagerWrapper.java
blob: 7c3c8d7eb9f1cf7532e045a5cfc2ebcf042f67b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * Copyright 2017-2018 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.aai.esr.wrapper;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Response;
import org.onap.aai.esr.entity.aai.CloudRegionDetail;
import org.onap.aai.esr.entity.aai.CloudRegionList;
import org.onap.aai.esr.entity.aai.ComplexList;
import org.onap.aai.esr.entity.aai.EsrSystemInfo;
import org.onap.aai.esr.entity.aai.Relationship;
import org.onap.aai.esr.entity.aai.RelationshipData;
import org.onap.aai.esr.entity.rest.VimRegisterInfo;
import org.onap.aai.esr.entity.rest.VimRegisterResponse;
import org.onap.aai.esr.exception.ExceptionUtil;
import org.onap.aai.esr.exception.ExtsysException;
import org.onap.aai.esr.externalservice.aai.CloudRegionProxy;
import org.onap.aai.esr.externalservice.cloud.Tenant;
import org.onap.aai.esr.externalservice.cloud.VimManagerProxy;
import org.onap.aai.esr.util.ExtsysUtil;
import org.onap.aai.esr.util.VimManagerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;

public class VimManagerWrapper {

    private static VimManagerWrapper vimManagerWrapper;
    private static final Logger LOG = LoggerFactory.getLogger(VimManagerWrapper.class);

    private static VimManagerUtil vimManagerUtil = new VimManagerUtil();

    private static CloudRegionProxy cloudRegionProxy = new CloudRegionProxy();

    private static ExtsysUtil extsysUtil = new ExtsysUtil();

    /**
     * get VimManagerWrapper instance.
     * 
     * @return vim manager wrapper instance
     */
    public static VimManagerWrapper getInstance() {
        if (vimManagerWrapper == null) {
            vimManagerWrapper = new VimManagerWrapper(cloudRegionProxy);
        }
        return vimManagerWrapper;
    }
    
    public VimManagerWrapper(CloudRegionProxy cloudRegionProxy) {
        VimManagerWrapper.cloudRegionProxy = cloudRegionProxy;
    }

    public Response registerVim(VimRegisterInfo vimRegisterInfo) {
        LOG.info("Start register VIM, input VIM info is: " + extsysUtil.objectToString(vimRegisterInfo));
        VimRegisterResponse result = new VimRegisterResponse();
        CloudRegionDetail cloudRegion = vimManagerUtil.vimRegisterInfo2CloudRegion(vimRegisterInfo);
        String cloudOwner = vimRegisterInfo.getCloudOwner();
        String cloudRegionId = vimRegisterInfo.getCloudRegionId();
        String physicalLocationId = vimRegisterInfo.getPhysicalLocationId();
        //TODO query complex by complex id to get complex name. and put the name to vimRegisterInfo.complexName
        try {
            cloudRegionProxy.registerVim(cloudOwner, cloudRegionId, cloudRegion);
            result.setCloudOwner(cloudOwner);
            result.setCloudRegionId(cloudRegionId);
            createRelationship(cloudOwner, cloudRegionId, physicalLocationId);
            updateVimWithMultiCloud(cloudRegion, cloudOwner, cloudRegionId);
            return Response.ok(result).build();
        } catch (ExtsysException error) {
            LOG.error("Register VIM failed !", error);
            throw ExceptionUtil.buildExceptionResponse(error.getMessage());
        }
    }

    private void updateVimWithMultiCloud(CloudRegionDetail cloudRegion, String cloudOwner, String cloudRegionId) {
        Tenant tenant = new Tenant();
        tenant.setDefaultTenant(cloudRegion.getEsrSystemInfoList().getEsrSystemInfo().get(0).getDefaultTenant());
        try {
            VimManagerProxy.updateVim(cloudOwner, cloudRegionId, tenant);
        } catch (ExtsysException e) {
            LOG.error("Update VIM by Multi-cloud failed !", e);
        }
    }
    
    private void createRelationship(String cloudOwner, String cloudRegionId, String physicalLocationId) {
        Relationship relationship = new Relationship();
        RelationshipData relationshipData = new RelationshipData();
        List<RelationshipData> relationshipDatas = new ArrayList<>();
        String relatedLink = "/aai/v11/cloud-infrastructure/complexes/complex/" + physicalLocationId;
        relationship.setRelatedTo("complex");
        relationship.setRelatedLink(relatedLink);
        relationshipData.setRelationshipKey("complex.physical-location-id");
        relationshipData.setRelationshipValue(physicalLocationId);
        relationshipDatas.add(relationshipData);
        relationship.setRelationshipData(relationshipDatas);
        try {
            cloudRegionProxy.createCloudRegionRelationShip(cloudOwner, cloudRegionId, relationship);
        } catch (ExtsysException e) {
            LOG.error("Create relationship between cloudRegion and complex failed !", e);
        }
    }

    public Response updateVim(String cloudOwner, String cloudRegionId, VimRegisterInfo vimRegisterInfo) {
        LOG.info("Start update VIM, input VIM info is: " + extsysUtil.objectToString(vimRegisterInfo));
        VimRegisterResponse result = new VimRegisterResponse();

        CloudRegionDetail cloudRegionDetail = getVimUpdateInfo(vimRegisterInfo);
        try {
            cloudRegionProxy.registerVim(cloudOwner, cloudRegionId, cloudRegionDetail);
            result.setCloudOwner(cloudOwner);
            result.setCloudRegionId(cloudRegionId);
            return Response.ok(result).build();
        } catch (ExtsysException e) {
            LOG.error("Update VIM failed !", e);
            throw ExceptionUtil.buildExceptionResponse(e.getMessage());
        }
    }

    public Response queryVimListDetails() {
        List<VimRegisterInfo> vimRegisterInfos = new ArrayList<>();
        CloudRegionList cloudRegionList = new CloudRegionList();
        try {
            String aaiVimList = cloudRegionProxy.qureyVimList();
            cloudRegionList = new Gson().fromJson(aaiVimList, CloudRegionList.class);
            vimRegisterInfos = getVimDetailList(cloudRegionList);
            return Response.ok(vimRegisterInfos).build();
        } catch (ExtsysException error) {
            LOG.error("Query vim list details failed !", error);
            return Response.ok(vimRegisterInfos).build();
        }

    }

    public Response queryVimById(String cloudOwner, String cloudRegionId) {
        VimRegisterInfo vim = new VimRegisterInfo();
        CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
        try {
            String cloudRegionstr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
            LOG.info("Response from AAI by query VIM: " + cloudRegionstr);
            cloudRegionDetail = new Gson().fromJson(cloudRegionstr, CloudRegionDetail.class);
            vim = vimManagerUtil.cloudRegion2VimRegisterInfo(cloudRegionDetail);
            return Response.ok(vim).build();
        } catch (ExtsysException e) {
            LOG.error("Query vim details by ID failed !", e);
            return Response.ok(vim).build();
        }

    }

    private List<VimRegisterInfo> getVimDetailList(CloudRegionList cloudRegionList) {
        List<VimRegisterInfo> vimRegisterInfos = new ArrayList<>();
        int cloudRegionNum = cloudRegionList.getCloudRegion().size();
        for (int i = 0; i < cloudRegionNum; i++) {
            String cloudOwner = cloudRegionList.getCloudRegion().get(i).getCloudOwner();
            String cloudRegionId = cloudRegionList.getCloudRegion().get(i).getCloudRegionId();
            VimRegisterInfo vimRegisterInfo = getVimDetail(cloudOwner, cloudRegionId);
            vimRegisterInfos.add(vimRegisterInfo);
        }
        return vimRegisterInfos;
    }

    private VimRegisterInfo getVimDetail(String cloudOwner, String cloudRegionId) {
        CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
        VimRegisterInfo registeredVimInfo = new VimRegisterInfo();
        try {
            String cloudRegionstr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
            cloudRegionDetail = new Gson().fromJson(cloudRegionstr, CloudRegionDetail.class);
            registeredVimInfo = vimManagerUtil.cloudRegion2VimRegisterInfo(cloudRegionDetail);
        } catch (ExtsysException error) {
            LOG.error("query VIM detail failed ! cloud-owner = " + cloudOwner + ", cloud-region-id = " + cloudRegionId,
                    error);
        }
        return registeredVimInfo;
    }

    private CloudRegionDetail getOriginalCloudRegion(String cloudOwner, String cloudRegionId) {
        CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
        try {
            String cloudRegionstr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
            cloudRegionDetail = new Gson().fromJson(cloudRegionstr, CloudRegionDetail.class);
            return cloudRegionDetail;
        } catch (ExtsysException error) {
            LOG.error("query VIM detail failed ! cloud-owner = " + cloudOwner + ", cloud-region-id = " + cloudRegionId,
                    error);
            return null;
        }
    }

    private CloudRegionDetail getVimUpdateInfo(VimRegisterInfo vimRegisterInfo) {
        String cloudOwner = vimRegisterInfo.getCloudOwner();
        String cloudRegionId = vimRegisterInfo.getCloudRegionId();
        CloudRegionDetail originalCloudRegionDetail = getOriginalCloudRegion(cloudOwner, cloudRegionId);
        String resourceVersion = originalCloudRegionDetail.getResourceVersion();
        CloudRegionDetail cloudRegionDetail = vimManagerUtil.vimRegisterInfo2CloudRegion(vimRegisterInfo);
        if (resourceVersion != null) {
            cloudRegionDetail.setResourceVersion(resourceVersion);
            EsrSystemInfo originalSystemInfo = originalCloudRegionDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
            cloudRegionDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
                    .setEsrSystemInfoId(originalSystemInfo.getEsrSystemInfoId());
            cloudRegionDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
                    .setResouceVersion(originalSystemInfo.getResouceVersion());
        }
        return cloudRegionDetail;
    }

    public Response delVim(String cloudOwner, String cloudRegionId) {
        CloudRegionDetail cloudRegionDetail = queryCloudRegionDetail(cloudOwner, cloudRegionId);
        String resourceVersion = cloudRegionDetail.getResourceVersion();
        try {
            cloudRegionProxy.deleteVim(cloudOwner, cloudRegionId, resourceVersion);
            return Response.noContent().build();
        } catch (ExtsysException e) {
            LOG.error("Delete cloud region from A&AI failed! cloud-owner = " + cloudOwner + ", cloud-region-id = "
                    + cloudRegionId + "resouce-version:" + resourceVersion, e);
            throw ExceptionUtil.buildExceptionResponse(e.getMessage());
        }
    }

    private CloudRegionDetail queryCloudRegionDetail(String cloudOwner, String cloudRegionId) {
        CloudRegionDetail cloudRegionDetail = new CloudRegionDetail();
        try {
            String cloudRegionStr = cloudRegionProxy.queryVimDetail(cloudOwner, cloudRegionId);
            LOG.info("Response from AAI by query cloud region: " + cloudRegionStr);
            cloudRegionDetail = new Gson().fromJson(cloudRegionStr, CloudRegionDetail.class);
        } catch (ExtsysException e) {
            LOG.error("Query EMS detail failed! cloud-owner = " + cloudOwner + ", cloud-region-id = " + cloudRegionId,
                    e);
            throw ExceptionUtil.buildExceptionResponse(e.getMessage());
        }
        return cloudRegionDetail;
    }

    public Response queryComplexes() {
        ComplexList complexList = new ComplexList();
        List<String> physicalLocationIdList = new ArrayList<>();
        try {
            String complexesString = cloudRegionProxy.qureyComplexes();
            LOG.info("The complex query result is: " + complexesString);
            complexList = new Gson().fromJson(complexesString, ComplexList.class);
            for (int i=0; i<complexList.getComplex().size(); i++) {
                physicalLocationIdList.add(complexList.getComplex().get(i).getPhysicalLocationId());
            }
        } catch (ExtsysException e) {
            LOG.error("Query vim details by ID failed !", e);
        }
        return Response.ok(physicalLocationIdList).build();
    }
}