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.squareup.okhttp.Credentials;
 
  24 import java.util.Collections;
 
  25 import org.onap.aai.domain.yang.EsrSystemInfo;
 
  26 import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
 
  27 import org.onap.so.adapters.vevnfm.subscription.SubscribeSender;
 
  28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
 
  29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthentication;
 
  30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic;
 
  31 import org.springframework.beans.factory.annotation.Autowired;
 
  32 import org.springframework.beans.factory.annotation.Value;
 
  33 import org.springframework.stereotype.Service;
 
  36 public class SubscriberService {
 
  38     private static final char COLON = ':';
 
  40     @Value("${system.url}")
 
  41     private String systemUrl;
 
  43     @Value("${server.port}")
 
  44     private String serverPort;
 
  46     @Value("${notification.url}")
 
  47     private String notificationUrl;
 
  49     @Value("${spring.security.usercredentials[0].username}")
 
  50     private String username;
 
  52     @Value("${spring.security.usercredentials[0].openpass}")
 
  53     private String openpass;
 
  56     private AuthorizationHeadersProvider headersProvider;
 
  59     private SubscribeSender sender;
 
  61     private static String getAuthorization(final EsrSystemInfo info) {
 
  62         return Credentials.basic(info.getUserName(), info.getPassword());
 
  65     public boolean subscribe(final EsrSystemInfo info) {
 
  67             headersProvider.addAuthorization(getAuthorization(info));
 
  68             final LccnSubscriptionRequest request = createRequest();
 
  69             return sender.send(info, request);
 
  71             headersProvider.removeAuthorization();
 
  75     private LccnSubscriptionRequest createRequest() {
 
  76         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
 
  77         request.callbackUri(getCallbackUri());
 
  78         final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
 
  79         final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
 
  80         paramsBasic.setUserName(username);
 
  81         paramsBasic.setPassword(openpass);
 
  82         authentication.setAuthType(Collections.singletonList(SubscriptionsAuthentication.AuthTypeEnum.BASIC));
 
  83         authentication.setParamsBasic(paramsBasic);
 
  84         request.authentication(authentication);
 
  89     private String getCallbackUri() {
 
  90         return systemUrl + COLON + serverPort + notificationUrl;