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.apache.logging.log4j.util.Strings;
26 import org.onap.aai.domain.yang.EsrSystemInfo;
27 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
28 import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthentication;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.stereotype.Service;
37 public class SubscriberService {
39 @Value("${vevnfmadapter.endpoint}")
40 private String endpoint;
42 @Value("${vnfm.notification}")
43 private String notification;
45 @Value("${spring.security.usercredentials[0].username}")
46 private String username;
48 @Value("${spring.security.usercredentials[0].openpass}")
49 private String openpass;
52 private AuthorizationHeadersProvider headersProvider;
55 private SubscribeSender sender;
57 private static String getAuthorization(final EsrSystemInfo info) {
62 final String userName = info.getUserName();
64 if (Strings.isBlank(userName)) {
68 final String password = info.getPassword();
69 return Credentials.basic(userName, password);
72 public String subscribe(final EsrSystemInfo info) throws VeVnfmException {
74 headersProvider.addAuthorization(getAuthorization(info));
75 final LccnSubscriptionRequest request = createRequest();
76 return sender.send(info, request);
77 } catch (Exception e) {
78 throw new VeVnfmException(e);
80 headersProvider.removeAuthorization();
84 public boolean checkSubscription(final EsrSystemInfo info, final String id) throws VeVnfmException {
86 return sender.check(info, id);
87 } catch (Exception e) {
88 throw new VeVnfmException(e);
92 private LccnSubscriptionRequest createRequest() {
93 final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
94 request.callbackUri(getCallbackUri());
95 final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
96 final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
97 paramsBasic.setUserName(username);
98 paramsBasic.setPassword(openpass);
99 authentication.setAuthType(Collections.singletonList(SubscriptionsAuthentication.AuthTypeEnum.BASIC));
100 authentication.setParamsBasic(paramsBasic);
101 request.authentication(authentication);
106 private String getCallbackUri() {
107 return endpoint + notification;