9760584d7afe9f0bbc95655b2b831758f3d39301
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.adapters.vevnfm.service;
22
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;
35
36 @Service
37 public class SubscriberService {
38
39     @Value("${vevnfmadapter.endpoint}")
40     private String endpoint;
41
42     @Value("${vnfm.notification}")
43     private String notification;
44
45     @Value("${spring.security.usercredentials[0].username}")
46     private String username;
47
48     @Value("${spring.security.usercredentials[0].openpass}")
49     private String openpass;
50
51     @Autowired
52     private AuthorizationHeadersProvider headersProvider;
53
54     @Autowired
55     private SubscribeSender sender;
56
57     private static String getAuthorization(final EsrSystemInfo info) {
58         if (info == null) {
59             return null;
60         }
61
62         final String userName = info.getUserName();
63
64         if (Strings.isBlank(userName)) {
65             return null;
66         }
67
68         final String password = info.getPassword();
69         return Credentials.basic(userName, password);
70     }
71
72     public String subscribe(final EsrSystemInfo info) throws VeVnfmException {
73         try {
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);
79         } finally {
80             headersProvider.removeAuthorization();
81         }
82     }
83
84     public boolean checkSubscription(final EsrSystemInfo info, final String id) throws VeVnfmException {
85         try {
86             return sender.check(info, id);
87         } catch (Exception e) {
88             throw new VeVnfmException(e);
89         }
90     }
91
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);
102
103         return request;
104     }
105
106     private String getCallbackUri() {
107         return endpoint + notification;
108     }
109 }