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.exception.VeVnfmException;
29 import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthentication;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.SubscriptionsFilter;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.stereotype.Service;
39 public class SubscriberService {
41 private static final Gson gson = new Gson();
43 @Value("${vevnfmadapter.vnf-filter-json}")
44 private String vnfFilter;
46 @Value("${vevnfmadapter.endpoint}")
47 private String endpoint;
49 @Value("${vnfm.notification}")
50 private String notification;
52 @Value("${spring.security.usercredentials[0].username}")
53 private String username;
55 @Value("${spring.security.usercredentials[0].openpass}")
56 private String openpass;
59 private AuthorizationHeadersProvider headersProvider;
62 private SubscribeSender sender;
64 private static String getAuthorization(final EsrSystemInfo info) {
69 final String userName = info.getUserName();
71 if (Strings.isBlank(userName)) {
75 final String password = info.getPassword();
76 return Credentials.basic(userName, password);
79 public String subscribe(final EsrSystemInfo info) throws VeVnfmException {
81 headersProvider.addAuthorization(getAuthorization(info));
82 final LccnSubscriptionRequest request = createRequest();
83 return sender.send(info, request);
84 } catch (Exception e) {
85 throw new VeVnfmException(e);
87 headersProvider.removeAuthorization();
91 public boolean checkSubscription(final EsrSystemInfo info, final String id) throws VeVnfmException {
93 return sender.check(info, id);
94 } catch (Exception e) {
95 throw new VeVnfmException(e);
99 private LccnSubscriptionRequest createRequest() {
100 final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
101 request.filter(getFilter());
102 request.callbackUri(getCallbackUri());
103 final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
104 final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
105 paramsBasic.setUserName(username);
106 paramsBasic.setPassword(openpass);
107 authentication.setAuthType(Collections.singletonList(SubscriptionsAuthentication.AuthTypeEnum.BASIC));
108 authentication.setParamsBasic(paramsBasic);
109 request.authentication(authentication);
114 private SubscriptionsFilter getFilter() {
115 return gson.fromJson(vnfFilter, SubscriptionsFilter.class);
118 private String getCallbackUri() {
119 return endpoint + notification;