Calls to/from VNFM fail
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / extclients / vnfm / VnfmServiceProviderImpl.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 com.google.common.base.Optional;
24 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
31 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
32 import org.onap.so.rest.service.HttpRestServiceProvider;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Qualifier;
37 import org.springframework.http.HttpStatus;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Service;
40
41 @Service
42 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
43     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
44
45     private final HttpRestServiceProvider httpServiceProvider;
46     private final VnfmUrlProvider urlProvider;
47
48     @Autowired
49     public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
50             @Qualifier("vnfmServiceProvider") final HttpRestServiceProvider httpServiceProvider) {
51         this.httpServiceProvider = httpServiceProvider;
52         this.urlProvider = urlProvider;
53     }
54
55     @Override
56     public Optional<InlineResponse201> getVnf(final String vnfSelfLink) {
57         return httpServiceProvider.get(vnfSelfLink.replaceAll("https", "http"), InlineResponse201.class);
58     }
59
60     @Override
61     public String instantiateVnf(final String vnfSelfLink, final InstantiateVnfRequest instantiateVnfRequest) {
62         logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
63
64         ResponseEntity<Void> response = null;
65         try {
66             response = httpServiceProvider.postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
67                     Void.class);
68         } catch (final Exception exception) {
69             final String errorMessage =
70                     "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
71             logger.error(errorMessage, exception);
72             throw new VnfmRequestFailureException(errorMessage, exception);
73         }
74         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
75             final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
76                     + response.getStatusCode() + ", request: " + instantiateVnfRequest;
77             logger.error(errorMessage);
78             throw new VnfmRequestFailureException(errorMessage);
79         }
80         final String locationHeader = response.getHeaders().get("Location").iterator().next();
81         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
82     }
83
84     @Override
85     public InlineResponse2001 subscribeForNotifications(final String vnfmId,
86             final LccnSubscriptionRequest subscriptionRequest) {
87         logger.info("Subscribing for notifications {}", subscriptionRequest);
88         final String url = urlProvider.getSubscriptionsUrl(vnfmId);
89         ResponseEntity<InlineResponse2001> response = null;
90         try {
91             response = httpServiceProvider.postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
92             logger.info("Subscribing for notifications response {}", response);
93         } catch (final Exception exception) {
94             final String errorMessage =
95                     "Subscription to VNFM " + vnfmId + " resulted in exception" + subscriptionRequest;
96             logger.error(errorMessage, exception);
97             throw new VnfmRequestFailureException(errorMessage, exception);
98         }
99         if (response.getStatusCode() != HttpStatus.CREATED) {
100             final String errorMessage = "Subscription to VNFM " + vnfmId + " returned status code: "
101                     + response.getStatusCode() + ", request: " + subscriptionRequest;
102             logger.error(errorMessage);
103             throw new VnfmRequestFailureException(errorMessage);
104         }
105         return response.getBody();
106     }
107
108     @Override
109     public String terminateVnf(final String vnfSelfLink, final TerminateVnfRequest terminateVnfRequest) {
110         logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
111
112         ResponseEntity<Void> response = null;
113         try {
114             response = httpServiceProvider.postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate", Void.class);
115         } catch (final Exception exception) {
116             final String errorMessage =
117                     "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
118             logger.error(errorMessage, exception);
119             throw new VnfmRequestFailureException(errorMessage, exception);
120         }
121         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
122             final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
123                     + response.getStatusCode() + ", request: " + terminateVnfRequest;
124             logger.error(errorMessage);
125             throw new VnfmRequestFailureException(errorMessage);
126         }
127         final String locationHeader = response.getHeaders().get("Location").iterator().next();
128         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
129
130     }
131
132     @Override
133     public void deleteVnf(final String vnfSelfLink) {
134         logger.debug("Sending delete request to : " + vnfSelfLink);
135         final ResponseEntity<Void> response = httpServiceProvider.deleteHttpRequest(vnfSelfLink, Void.class);
136         if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
137             throw new VnfmRequestFailureException(
138                     "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
139         }
140     }
141
142     @Override
143     public Optional<InlineResponse200> getOperation(final String vnfmId, final String operationId) {
144         final String url = urlProvider.getOperationUrl(vnfmId, operationId);
145         return httpServiceProvider.get(url, InlineResponse200.class);
146     }
147
148     @Override
149     public Optional<InlineResponse201> createVnf(final String vnfmId, final CreateVnfRequest createVnfRequest) {
150         final String url = urlProvider.getCreationUrl(vnfmId);
151         logger.debug("Sending create request {} to : {}", createVnfRequest, url);
152         try {
153             return httpServiceProvider.post(createVnfRequest, url, InlineResponse201.class);
154         } catch (final Exception exception) {
155             final String errorMessage =
156                     "Create request to vnfm:" + vnfmId + " resulted in exception" + createVnfRequest;
157             logger.error(errorMessage, exception);
158             throw new VnfmRequestFailureException(errorMessage, exception);
159         }
160     }
161
162 }