f5656f75369b3b00fc8814c891092b10dc0e3e09
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / AAIExternalSystemInfoProvider.java
1 /*
2  * Copyright 2016-2017, Nokia 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.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct;
17
18 import org.onap.aai.domain.yang.v11.EsrSystemInfo;
19 import org.onap.aai.domain.yang.v11.EsrSystemInfoList;
20 import org.onap.aai.domain.yang.v11.EsrVnfm;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.GenericExternalSystemInfoProvider;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
23 import org.onap.vnfmdriver.model.VimInfo;
24 import org.onap.vnfmdriver.model.VnfmInfo;
25 import org.slf4j.Logger;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Conditional;
28 import org.springframework.core.env.Environment;
29 import org.springframework.stereotype.Component;
30
31 import static java.lang.String.format;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.CLOUD;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.ESR;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
37 import static org.slf4j.LoggerFactory.getLogger;
38
39 /**
40  * Responsible for providing information related to the VNFM from VF-C source
41  */
42 @Component
43 @Conditional(value = Conditions.UseForDirect.class)
44 public class AAIExternalSystemInfoProvider extends GenericExternalSystemInfoProvider {
45     private static final String VNFM_URL = "/esr-vnfm-list/esr-vnfm/%s?depth=all";
46     private static final String VIM_URL = "/cloud-regions/cloud-region/%s/%s/esr-system-info-list";
47     private static Logger logger = getLogger(AAIExternalSystemInfoProvider.class);
48     private final AAIRestApiProvider aaiRestApiProvider;
49
50     @Autowired
51     AAIExternalSystemInfoProvider(Environment environment, AAIRestApiProvider aaiRestApiProvider) {
52         super(environment);
53         this.aaiRestApiProvider = aaiRestApiProvider;
54     }
55
56     @Override
57     public VnfmInfo queryVnfmInfoFromSource(String vnfmId) {
58         try {
59             return convertEsrToVnfmInfo(aaiRestApiProvider.get(logger, ESR, format(VNFM_URL, vnfmId), EsrVnfm.class));
60         } catch (RuntimeException e) {
61             throw buildFatalFailure(logger, "Unable to query VNFM with " + vnfmId + " identifier from AAI", e);
62         }
63     }
64
65     @Override
66     public VimInfo getVimInfo(String vimId) {
67         try {
68             return convertEsrToVim(getEsrSystemInfo(vimId), vimId);
69         } catch (RuntimeException e) {
70             throw buildFatalFailure(logger, "Unable to query VIM with " + vimId + " identifier from AAI", e);
71         }
72     }
73
74     /**
75      * @param vimId the identifier of the VIM
76      * @return the VIM details
77      */
78     public EsrSystemInfo getEsrSystemInfo(String vimId) {
79         String url = format(VIM_URL, getCloudOwner(vimId), getRegionName(vimId));
80         return aaiRestApiProvider.get(logger, CLOUD, url, EsrSystemInfoList.class).getEsrSystemInfo().get(0);
81     }
82
83     private VimInfo convertEsrToVim(EsrSystemInfo esrSystemInfo, String vimId) {
84         VimInfo vimInfo = new VimInfo();
85         vimInfo.setDescription(esrSystemInfo.getSystemName());
86         vimInfo.setName(esrSystemInfo.getSystemName());
87         vimInfo.setPassword(esrSystemInfo.getPassword());
88         vimInfo.setStatus(esrSystemInfo.getSystemStatus());
89         vimInfo.setType(esrSystemInfo.getType());
90         vimInfo.setUrl(esrSystemInfo.getServiceUrl());
91         vimInfo.setVersion(esrSystemInfo.getVersion());
92         if (esrSystemInfo.getSslCacert() == null) {
93             vimInfo.setSslInsecure("true");
94         } else {
95             vimInfo.setSslInsecure("false");
96             vimInfo.setSslCacert(esrSystemInfo.getSslCacert());
97         }
98         vimInfo.setUserName(esrSystemInfo.getUserName());
99         vimInfo.setVendor(esrSystemInfo.getVendor());
100         vimInfo.setVimId(vimId);
101         return vimInfo;
102     }
103
104
105     private VnfmInfo convertEsrToVnfmInfo(EsrVnfm vnfmInAai) {
106         EsrSystemInfo esrSystemInfo = vnfmInAai.getEsrSystemInfoList().getEsrSystemInfo().get(0);
107         VnfmInfo vnfmInfo = new VnfmInfo();
108         vnfmInfo.setPassword(esrSystemInfo.getPassword());
109         vnfmInfo.setDescription(esrSystemInfo.getEsrSystemInfoId());
110         vnfmInfo.setName(esrSystemInfo.getSystemName());
111         vnfmInfo.setType(esrSystemInfo.getType());
112         vnfmInfo.setUrl(esrSystemInfo.getServiceUrl());
113         vnfmInfo.setVersion(esrSystemInfo.getVersion());
114         vnfmInfo.setVimId(vnfmInAai.getVimId());
115         vnfmInfo.setVendor(esrSystemInfo.getVendor());
116         vnfmInfo.setUserName(esrSystemInfo.getUserName());
117         vnfmInfo.setVnfmId(vnfmInAai.getVnfmId());
118         return vnfmInfo;
119     }
120 }