2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 Samsung. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.adapters.vevnfm.service;
 
  23 import com.google.gson.Gson;
 
  24 import com.squareup.okhttp.Credentials;
 
  25 import java.util.Collections;
 
  26 import org.apache.logging.log4j.util.Strings;
 
  27 import org.onap.aai.domain.yang.EsrSystemInfo;
 
  28 import org.onap.so.adapters.vevnfm.configuration.ConfigProperties;
 
  29 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
 
  30 import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
 
  31 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.LccnSubscriptionRequest;
 
  32 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.SubscriptionsAuthentication;
 
  33 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic;
 
  34 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.SubscriptionsFilter;
 
  35 import org.springframework.beans.factory.annotation.Autowired;
 
  36 import org.springframework.stereotype.Service;
 
  39 public class SubscriberService {
 
  41     private static final Gson gson = new Gson();
 
  43     private final String vnfFilter;
 
  44     private final String endpoint;
 
  45     private final String notification;
 
  46     private final String username;
 
  47     private final String openpass;
 
  48     private final AuthorizationHeadersProvider headersProvider;
 
  49     private final SubscribeSender sender;
 
  52     public SubscriberService(final ConfigProperties configProperties,
 
  53             final AuthorizationHeadersProvider headersProvider, final SubscribeSender sender) {
 
  54         this.vnfFilter = configProperties.getVevnfmadapterVnfFilterJson();
 
  55         this.endpoint = configProperties.getVevnfmadapterEndpoint();
 
  56         this.notification = configProperties.getVnfmNotification();
 
  57         this.username = configProperties.getSpringSecurityUsername();
 
  58         this.openpass = configProperties.getSpringSecurityOpenpass();
 
  59         this.headersProvider = headersProvider;
 
  63     private static String getAuthorization(final EsrSystemInfo info) {
 
  68         final String userName = info.getUserName();
 
  70         if (Strings.isBlank(userName)) {
 
  74         final String password = info.getPassword();
 
  75         return Credentials.basic(userName, password);
 
  78     public String subscribe(final EsrSystemInfo info) throws VeVnfmException {
 
  80             headersProvider.addAuthorization(getAuthorization(info));
 
  81             final LccnSubscriptionRequest request = createRequest();
 
  82             return sender.send(info, request);
 
  83         } catch (Exception e) {
 
  84             throw new VeVnfmException(e);
 
  86             headersProvider.removeAuthorization();
 
  90     public boolean checkSubscription(final EsrSystemInfo info, final String id) throws VeVnfmException {
 
  92             return sender.check(info, id);
 
  93         } catch (Exception e) {
 
  94             throw new VeVnfmException(e);
 
  98     private LccnSubscriptionRequest createRequest() {
 
  99         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
 
 100         request.filter(getFilter());
 
 101         request.callbackUri(getCallbackUri());
 
 102         final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
 
 103         final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
 
 104         paramsBasic.setUserName(username);
 
 105         paramsBasic.setPassword(openpass);
 
 106         authentication.setAuthType(Collections.singletonList(SubscriptionsAuthentication.AuthTypeEnum.BASIC));
 
 107         authentication.setParamsBasic(paramsBasic);
 
 108         request.authentication(authentication);
 
 113     private SubscriptionsFilter getFilter() {
 
 114         return gson.fromJson(vnfFilter, SubscriptionsFilter.class);
 
 117     private String getCallbackUri() {
 
 118         return endpoint + notification;