Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / mso-ve-vnfm-adapter / src / main / java / org / onap / so / adapters / vevnfm / service / SubscriberService.java
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.configuration.ConfigProperties;
29 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
30 import org.onap.so.adapters.vevnfm.provider.AuthorizationHeadersProvider;
31 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.LccnSubscriptionRequest;
32 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.SubscriptionsAuthentication;
33 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.SubscriptionsAuthenticationParamsBasic;
34 import org.onap.so.adapters.etsi.sol003.adapter.lcm.extclients.vnfm.model.SubscriptionsFilter;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 @Service
39 public class SubscriberService {
40
41     private static final Gson gson = new Gson();
42
43     private final String vnfFilter;
44     private final String endpoint;
45     private final String notification;
46     private final String username;
47     private final String openpass;
48     private final AuthorizationHeadersProvider headersProvider;
49     private final SubscribeSender sender;
50
51     @Autowired
52     public SubscriberService(final ConfigProperties configProperties,
53             final AuthorizationHeadersProvider headersProvider, final SubscribeSender sender) {
54         this.vnfFilter = configProperties.getVevnfmadapterVnfFilterJson();
55         this.endpoint = configProperties.getVevnfmadapterEndpoint();
56         this.notification = configProperties.getVnfmNotification();
57         this.username = configProperties.getSpringSecurityUsername();
58         this.openpass = configProperties.getSpringSecurityOpenpass();
59         this.headersProvider = headersProvider;
60         this.sender = sender;
61     }
62
63     private static String getAuthorization(final EsrSystemInfo info) {
64         if (info == null) {
65             return null;
66         }
67
68         final String userName = info.getUserName();
69
70         if (Strings.isBlank(userName)) {
71             return null;
72         }
73
74         final String password = info.getPassword();
75         return Credentials.basic(userName, password);
76     }
77
78     public String subscribe(final EsrSystemInfo info) throws VeVnfmException {
79         try {
80             headersProvider.addAuthorization(getAuthorization(info));
81             final LccnSubscriptionRequest request = createRequest();
82             return sender.send(info, request);
83         } catch (Exception e) {
84             throw new VeVnfmException(e);
85         } finally {
86             headersProvider.removeAuthorization();
87         }
88     }
89
90     public boolean checkSubscription(final EsrSystemInfo info, final String id) throws VeVnfmException {
91         try {
92             return sender.check(info, id);
93         } catch (Exception e) {
94             throw new VeVnfmException(e);
95         }
96     }
97
98     private LccnSubscriptionRequest createRequest() {
99         final LccnSubscriptionRequest request = new LccnSubscriptionRequest();
100         request.filter(getFilter());
101         request.callbackUri(getCallbackUri());
102         final SubscriptionsAuthenticationParamsBasic paramsBasic = new SubscriptionsAuthenticationParamsBasic();
103         final SubscriptionsAuthentication authentication = new SubscriptionsAuthentication();
104         paramsBasic.setUserName(username);
105         paramsBasic.setPassword(openpass);
106         authentication.setAuthType(Collections.singletonList(SubscriptionsAuthentication.AuthTypeEnum.BASIC));
107         authentication.setParamsBasic(paramsBasic);
108         request.authentication(authentication);
109
110         return request;
111     }
112
113     private SubscriptionsFilter getFilter() {
114         return gson.fromJson(vnfFilter, SubscriptionsFilter.class);
115     }
116
117     private String getCallbackUri() {
118         return endpoint + notification;
119     }
120 }