Send instantiate VNF Request to VNFM
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / vnfm / VnfmUrlProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.extclients.vnfm;
22
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.net.URI;
25 import org.onap.aai.domain.yang.EsrSystemInfo;
26 import org.onap.aai.domain.yang.EsrSystemInfoList;
27 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
28 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
29 import org.slf4j.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Service;
32 import org.springframework.web.util.UriComponentsBuilder;
33
34 /**
35  * Provides URLs for REST calls to a VNFM.
36  */
37 @Service
38 public class VnfmUrlProvider {
39
40     private static Logger logger = getLogger(VnfmUrlProvider.class);
41     private final AaiServiceProvider aaiServiceProvider;
42
43     @Autowired
44     public VnfmUrlProvider(final AaiServiceProvider aaiServiceProvider) {
45         this.aaiServiceProvider = aaiServiceProvider;
46     }
47
48     /**
49      * Get the URL for an operation on a VNFM.
50      *
51      * @param vnfmId The ID of the VNFM
52      * @return the URL of the operation
53      */
54     public String getOperationUrl(final String vnfmId, final String operationId) {
55         final String url = UriComponentsBuilder.fromUri(getBaseUri(vnfmId)).pathSegment("/vnf_lcm_op_occs/")
56                 .pathSegment(operationId).build().toString();
57         logger.debug("getOperationUrl:" + url);
58
59         return url;
60     }
61
62     private URI getBaseUri(final String vnfmId) {
63         final EsrSystemInfoList vnfmEsrSystemInfoList = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfmId);
64
65         if (vnfmEsrSystemInfoList != null) {
66             for (final EsrSystemInfo esrSystemInfo : vnfmEsrSystemInfoList.getEsrSystemInfo()) {
67                 return UriComponentsBuilder.fromHttpUrl(esrSystemInfo.getServiceUrl()).build().toUri();
68             }
69         }
70
71         throw new VnfmNotFoundException("VNFM, or Service URL for VNFM, not found for VNFM " + vnfmId);
72     }
73 }