0e77ce40734f443b9b549024d4a8a2ea3d63a3c0
[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.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;
34
35 @Service
36 public class SubscriberService {
37
38     private static final char COLON = ':';
39
40     @Value("${system.url}")
41     private String systemUrl;
42
43     @Value("${server.port}")
44     private String serverPort;
45
46     @Value("${notification.url}")
47     private String notificationUrl;
48
49     @Value("${spring.security.usercredentials[0].username}")
50     private String username;
51
52     @Value("${spring.security.usercredentials[0].openpass}")
53     private String openpass;
54
55     @Autowired
56     private AuthorizationHeadersProvider headersProvider;
57
58     @Autowired
59     private SubscribeSender sender;
60
61     private static String getAuthorization(final EsrSystemInfo info) {
62         return Credentials.basic(info.getUserName(), info.getPassword());
63     }
64
65     public boolean subscribe(final EsrSystemInfo info) {
66         try {
67             headersProvider.addAuthorization(getAuthorization(info));
68             final LccnSubscriptionRequest request = createRequest();
69             return sender.send(info, request);
70         } finally {
71             headersProvider.resetPrevious();
72         }
73     }
74
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);
85
86         return request;
87     }
88
89     private String getCallbackUri() {
90         return systemUrl + COLON + serverPort + notificationUrl;
91     }
92 }