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
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.adapters.vnfmadapter.extclients.vnfm;
 
  23 import com.google.common.base.Optional;
 
  24 import org.onap.aai.domain.yang.EsrVnfm;
 
  25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
 
  26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
 
  27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse2001;
 
  28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
 
  29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
 
  30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
 
  31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
 
  32 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmRequestFailureException;
 
  33 import org.onap.so.rest.service.HttpRestServiceProvider;
 
  34 import org.slf4j.Logger;
 
  35 import org.slf4j.LoggerFactory;
 
  36 import org.springframework.beans.factory.annotation.Autowired;
 
  37 import org.springframework.http.HttpStatus;
 
  38 import org.springframework.http.ResponseEntity;
 
  39 import org.springframework.stereotype.Service;
 
  42 public class VnfmServiceProviderImpl implements VnfmServiceProvider {
 
  43     private static final Logger logger = LoggerFactory.getLogger(VnfmServiceProviderImpl.class);
 
  45     private final VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration;
 
  46     private final VnfmUrlProvider urlProvider;
 
  49     public VnfmServiceProviderImpl(final VnfmUrlProvider urlProvider,
 
  50             VnfmServiceProviderConfiguration vnfmServiceProviderConfiguration) {
 
  51         this.vnfmServiceProviderConfiguration = vnfmServiceProviderConfiguration;
 
  52         this.urlProvider = urlProvider;
 
  56     public Optional<InlineResponse201> getVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
 
  57         return getHttpServiceProvider(vnfm).get(vnfSelfLink, InlineResponse201.class);
 
  61     public String instantiateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
 
  62             final InstantiateVnfRequest instantiateVnfRequest) {
 
  63         logger.debug("Sending instantiate request " + instantiateVnfRequest + " to : " + vnfSelfLink);
 
  65         ResponseEntity<Void> response = null;
 
  67             response = getHttpServiceProvider(vnfm).postHttpRequest(instantiateVnfRequest, vnfSelfLink + "/instantiate",
 
  69         } catch (final Exception exception) {
 
  70             final String errorMessage =
 
  71                     "Instantiate request to " + vnfSelfLink + " resulted in exception" + instantiateVnfRequest;
 
  72             logger.error(errorMessage, exception);
 
  73             throw new VnfmRequestFailureException(errorMessage, exception);
 
  75         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
 
  76             final String errorMessage = "Instantiate request to " + vnfSelfLink + " returned status code: "
 
  77                     + response.getStatusCode() + ", request: " + instantiateVnfRequest;
 
  78             logger.error(errorMessage);
 
  79             throw new VnfmRequestFailureException(errorMessage);
 
  81         final String locationHeader = response.getHeaders().get("Location").iterator().next();
 
  82         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
 
  86     public InlineResponse2001 subscribeForNotifications(final EsrVnfm vnfm,
 
  87             final LccnSubscriptionRequest subscriptionRequest) {
 
  88         logger.info("Subscribing for notifications {}", subscriptionRequest);
 
  89         final String url = urlProvider.getSubscriptionsUrl(vnfm.getVnfmId());
 
  90         ResponseEntity<InlineResponse2001> response = null;
 
  92             response = getHttpServiceProvider(vnfm).postHttpRequest(subscriptionRequest, url, InlineResponse2001.class);
 
  93             logger.info("Subscribing for notifications response {}", response);
 
  94         } catch (final Exception exception) {
 
  95             final String errorMessage =
 
  96                     "Subscription to VNFM " + vnfm.getVnfmId() + " resulted in exception" + subscriptionRequest;
 
  97             logger.error(errorMessage, exception);
 
  98             throw new VnfmRequestFailureException(errorMessage, exception);
 
 100         if (response.getStatusCode() != HttpStatus.CREATED) {
 
 101             final String errorMessage = "Subscription to VNFM " + vnfm.getVnfmId() + " returned status code: "
 
 102                     + response.getStatusCode() + ", request: " + subscriptionRequest;
 
 103             logger.error(errorMessage);
 
 104             throw new VnfmRequestFailureException(errorMessage);
 
 106         return response.getBody();
 
 110     public String terminateVnf(final EsrVnfm vnfm, final String vnfSelfLink,
 
 111             final TerminateVnfRequest terminateVnfRequest) {
 
 112         logger.debug("Sending terminate request " + terminateVnfRequest + " to : " + vnfSelfLink);
 
 114         ResponseEntity<Void> response = null;
 
 116             response = getHttpServiceProvider(vnfm).postHttpRequest(terminateVnfRequest, vnfSelfLink + "/terminate",
 
 118         } catch (final Exception exception) {
 
 119             final String errorMessage =
 
 120                     "Terminate request to " + vnfSelfLink + " resulted in exception" + terminateVnfRequest;
 
 121             logger.error(errorMessage, exception);
 
 122             throw new VnfmRequestFailureException(errorMessage, exception);
 
 124         if (response.getStatusCode() != HttpStatus.ACCEPTED) {
 
 125             final String errorMessage = "Terminate request to " + vnfSelfLink + " returned status code: "
 
 126                     + response.getStatusCode() + ", request: " + terminateVnfRequest;
 
 127             logger.error(errorMessage);
 
 128             throw new VnfmRequestFailureException(errorMessage);
 
 130         final String locationHeader = response.getHeaders().get("Location").iterator().next();
 
 131         return locationHeader.substring(locationHeader.lastIndexOf("/") + 1);
 
 136     public void deleteVnf(final EsrVnfm vnfm, final String vnfSelfLink) {
 
 137         logger.debug("Sending delete request to : " + vnfSelfLink);
 
 138         final ResponseEntity<Void> response = getHttpServiceProvider(vnfm).deleteHttpRequest(vnfSelfLink, Void.class);
 
 139         if (response.getStatusCode() != HttpStatus.NO_CONTENT) {
 
 140             throw new VnfmRequestFailureException(
 
 141                     "Delete request to " + vnfSelfLink + " return status code: " + response.getStatusCode());
 
 146     public Optional<InlineResponse200> getOperation(final EsrVnfm vnfm, final String operationId) {
 
 147         final String url = urlProvider.getOperationUrl(vnfm.getVnfmId(), operationId);
 
 148         return getHttpServiceProvider(vnfm).get(url, InlineResponse200.class);
 
 152     public Optional<InlineResponse201> createVnf(final EsrVnfm vnfm, final CreateVnfRequest createVnfRequest) {
 
 153         final String url = urlProvider.getCreationUrl(vnfm.getVnfmId());
 
 154         logger.debug("Sending create request {} to : {}", createVnfRequest, url);
 
 156             return getHttpServiceProvider(vnfm).post(createVnfRequest, url, InlineResponse201.class);
 
 157         } catch (final Exception exception) {
 
 158             final String errorMessage =
 
 159                     "Create request to vnfm:" + vnfm.getVnfmId() + " resulted in exception" + createVnfRequest;
 
 160             logger.error(errorMessage, exception);
 
 161             throw new VnfmRequestFailureException(errorMessage, exception);
 
 165     private HttpRestServiceProvider getHttpServiceProvider(final EsrVnfm vnfm) {
 
 166         return vnfmServiceProviderConfiguration.getHttpRestServiceProvider(vnfm);