Merge "BB workflow with post instantiation is not working"
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-lcm / etsi-sol003-lcm-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     /**
63      * Get the URL for the subscriptions on a VNFM.
64      *
65      * @param vnfmId The ID of the VNFM
66      * @return the URL of the subscriptions
67      */
68     public String getSubscriptionsUrl(final String vnfmId) {
69         final String url =
70                 UriComponentsBuilder.fromUri(getBaseUri(vnfmId)).pathSegment("subscriptions").build().toString();
71         logger.debug("getSubscriptionUrl:" + url);
72
73         return url;
74     }
75
76     public String getCreationUrl(final String vnfmId) {
77         final String url =
78                 UriComponentsBuilder.fromUri(getBaseUri(vnfmId)).pathSegment("vnf_instances").build().toString();
79         logger.debug("getCreationUrl:" + url);
80
81         return url;
82     }
83
84     private URI getBaseUri(final String vnfmId) {
85         final EsrSystemInfoList vnfmEsrSystemInfoList = aaiServiceProvider.invokeGetVnfmEsrSystemInfoList(vnfmId);
86
87         if (vnfmEsrSystemInfoList != null) {
88             for (final EsrSystemInfo esrSystemInfo : vnfmEsrSystemInfoList.getEsrSystemInfo()) {
89                 return UriComponentsBuilder.fromHttpUrl(esrSystemInfo.getServiceUrl()).build().toUri();
90             }
91         }
92
93         throw new VnfmNotFoundException("VNFM, or Service URL for VNFM, not found for VNFM " + vnfmId);
94     }
95 }