d2cf4833ba0e368ad8514a349e7d5ee67b94ac6e
[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.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;
37
38 @Service
39 public class SubscriberService {
40
41     private static final Gson gson = new Gson();
42
43     @Value("${vevnfmadapter.vnf-filter-json}")
44     private String vnfFilter;
45
46     @Value("${vevnfmadapter.endpoint}")
47     private String endpoint;
48
49     @Value("${vnfm.notification}")
50     private String notification;
51
52     @Value("${spring.security.usercredentials[0].username}")
53     private String username;
54
55     @Value("${spring.security.usercredentials[0].openpass}")
56     private String openpass;
57
58     @Autowired
59     private AuthorizationHeadersProvider headersProvider;
60
61     @Autowired
62     private SubscribeSender sender;
63
64     private static String getAuthorization(final EsrSystemInfo info) {
65         if (info == null) {
66             return null;
67         }
68
69         final String userName = info.getUserName();
70
71         if (Strings.isBlank(userName)) {
72             return null;
73         }
74
75         final String password = info.getPassword();
76         return Credentials.basic(userName, password);
77     }
78
79     public String subscribe(final EsrSystemInfo info) throws VeVnfmException {
80         try {
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);
86         } finally {
87             headersProvider.removeAuthorization();
88         }
89     }
90
91     public boolean checkSubscription(final EsrSystemInfo info, final String id) throws VeVnfmException {
92         try {
93             return sender.check(info, id);
94         } catch (Exception e) {
95             throw new VeVnfmException(e);
96         }
97     }
98
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);
110
111         return request;
112     }
113
114     private SubscriptionsFilter getFilter() {
115         return gson.fromJson(vnfFilter, SubscriptionsFilter.class);
116     }
117
118     private String getCallbackUri() {
119         return endpoint + notification;
120     }
121 }