efe747deae050eb55ff71ef3be3d1f710bb1991c
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / packagemanagement / subscriptionmanagement / SubscriptionManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement;
22
23 import static org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.SubscriptionAuthentication.AuthTypeEnum.BASIC;
24 import static org.slf4j.LoggerFactory.getLogger;
25 import java.net.URI;
26 import java.security.GeneralSecurityException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Objects;
31 import java.util.Optional;
32 import org.onap.so.adapters.vnfmadapter.Constants;
33 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogServiceProvider;
34 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.BasicAuth;
35 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2002;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
38 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsLinks;
39 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinksSelf;
40 import org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.cache.PackageManagementCacheServiceProvider;
41 import org.onap.so.adapters.vnfmadapter.rest.exceptions.InternalServerErrorException;
42 import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionRequestConversionException;
43 import org.onap.so.utils.CryptoUtils;
44 import org.slf4j.Logger;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.core.convert.ConversionException;
48 import org.springframework.core.convert.ConversionService;
49 import org.springframework.stereotype.Service;
50
51 /**
52  * Manages package management subscriptions from the VNFMs
53  *
54  * @author Ronan Kenny (ronan.kenny@est.tech)
55  * @author Gareth Roper (gareth.roper@est.tech)
56  */
57 @Service
58 public class SubscriptionManager {
59
60     private static final Logger logger = getLogger(SubscriptionManager.class);
61     private final PackageManagementCacheServiceProvider packageManagementCacheServiceProvider;
62     private final ConversionService conversionService;
63     private final EtsiCatalogServiceProvider etsiCatalogServiceProvider;
64     private final String vnfmAdapterEndpoint;
65     private final String msoKeyString;
66     private final String vnfmAdapterAuth;
67
68
69     @Autowired
70     public SubscriptionManager(final PackageManagementCacheServiceProvider packageManagementCacheServiceProvider,
71             final ConversionService conversionService, final EtsiCatalogServiceProvider etsiCatalogServiceProvider,
72             @Value("${vnfmadapter.endpoint}") final String vnfmAdapterEndpoint,
73             @Value("${mso.key}") final String msoKeyString,
74             @Value("${vnfmadapter.auth:BF29BA36F0CFE1C05507781F6B97EFBCA7EFAC9F595954D465FC43F646883EF585C20A58CBB02528A6FAAC}") final String vnfmAdapterAuth) {
75         this.packageManagementCacheServiceProvider = packageManagementCacheServiceProvider;
76         this.conversionService = conversionService;
77         this.etsiCatalogServiceProvider = etsiCatalogServiceProvider;
78         this.vnfmAdapterEndpoint = vnfmAdapterEndpoint;
79         this.vnfmAdapterAuth = vnfmAdapterAuth;
80         this.msoKeyString = msoKeyString;
81     }
82
83     public Optional<InlineResponse2002> createSubscription(final PkgmSubscriptionRequest pkgmSubscriptionRequest)
84             throws GeneralSecurityException {
85
86         final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest =
87                 buildEtsiCatalogManagerPkgmSubscriptionRequest(pkgmSubscriptionRequest);
88
89         final Optional<PkgmSubscription> optionalEtsiCatalogManagerSubscription =
90                 etsiCatalogServiceProvider.postSubscription(etsiCatalogManagerSubscriptionRequest);
91
92         if (optionalEtsiCatalogManagerSubscription.isPresent()) {
93             PkgmSubscription etsiCatalogManagerSubscription = optionalEtsiCatalogManagerSubscription.get();
94             logger.debug("postPkgmSubscriptionRequest Response SubscriptionId: {}",
95                     Objects.requireNonNull(etsiCatalogManagerSubscription.getId()));
96             final String subscriptionId = etsiCatalogManagerSubscription.getId();
97
98             packageManagementCacheServiceProvider.addSubscription(subscriptionId, pkgmSubscriptionRequest);
99
100             final InlineResponse2002 response2002 = new InlineResponse2002();
101             response2002.setId(subscriptionId);
102             response2002.setFilter(pkgmSubscriptionRequest.getFilter());
103             response2002.setCallbackUri(getSubscriptionUri(subscriptionId).toString());
104             response2002.setLinks(new SubscriptionsLinks()
105                     .self(new VnfPackagesLinksSelf().href(getSubscriptionUri(subscriptionId).toString())));
106
107             return Optional.of(response2002);
108         }
109         throw new InternalServerErrorException(
110                 "Received empty response from POST to ETSI Catalog Manager Subscription Endpoint.");
111     }
112
113
114     public Optional<String> getSubscriptionId(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
115         return packageManagementCacheServiceProvider.getSubscriptionId(pkgmSubscriptionRequest);
116     }
117
118     public Optional<InlineResponse2002> getSubscription(final String subscriptionId) {
119         final Optional<PkgmSubscriptionRequest> optional =
120                 packageManagementCacheServiceProvider.getSubscription(subscriptionId);
121         if (optional.isPresent()) {
122             final PkgmSubscriptionRequest subscription = optional.get();
123             return Optional.of(getInlineResponse2002(subscriptionId, subscription));
124         }
125         return Optional.empty();
126     }
127
128     public List<InlineResponse2002> getSubscriptions() {
129         final Map<String, PkgmSubscriptionRequest> subscriptions =
130                 packageManagementCacheServiceProvider.getSubscriptions();
131         final List<InlineResponse2002> response = new ArrayList<>();
132         subscriptions.forEach((key, value) -> response.add(getInlineResponse2002(key, value)));
133         return response;
134     }
135
136     public boolean deleteSubscription(final String subscriptionId) {
137         if (getSubscription(subscriptionId).isPresent()) {
138             if (etsiCatalogServiceProvider.deleteSubscription(subscriptionId)) {
139                 return packageManagementCacheServiceProvider.deleteSubscription(subscriptionId);
140             }
141         }
142         return false;
143     }
144
145     public URI getSubscriptionUri(final String subscriptionId) {
146         return URI.create(
147                 vnfmAdapterEndpoint + Constants.PACKAGE_MANAGEMENT_BASE_URL + "/subscriptions/" + subscriptionId);
148     }
149
150     private InlineResponse2002 getInlineResponse2002(final String id, final PkgmSubscriptionRequest subscription) {
151         return new InlineResponse2002().id(id).filter(subscription.getFilter())
152                 .callbackUri(subscription.getCallbackUri());
153     }
154
155     private org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest buildEtsiCatalogManagerPkgmSubscriptionRequest(
156             PkgmSubscriptionRequest pkgmSubscriptionRequest) throws GeneralSecurityException {
157
158         final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest;
159         try {
160             etsiCatalogManagerSubscriptionRequest = conversionService.convert(pkgmSubscriptionRequest,
161                     org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest.class);
162         } catch (ConversionException conversionException) {
163             logger.error(conversionException.getMessage());
164             throw new SubscriptionRequestConversionException(
165                     "Could not convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest");
166         } catch (Exception exception) {
167             logger.error(exception.getMessage());
168             throw new InternalServerErrorException(
169                     "Could not convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest");
170         }
171
172         if (etsiCatalogManagerSubscriptionRequest != null) {
173             etsiCatalogManagerSubscriptionRequest
174                     .setCallbackUri(vnfmAdapterEndpoint + Constants.ETSI_SUBSCRIPTION_NOTIFICATION_BASE_URL);
175
176             final String[] auth = decryptAuth();
177             final String username = auth[0];
178             final String password = auth[1];
179
180             etsiCatalogManagerSubscriptionRequest.setAuthentication(
181                     new org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.SubscriptionAuthentication()
182                             .addAuthTypeItem(BASIC).paramsBasic(new BasicAuth().userName(username).password(password)));
183             return etsiCatalogManagerSubscriptionRequest;
184         }
185         throw new SubscriptionRequestConversionException(
186                 "Failed to convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest");
187     }
188
189     private String[] decryptAuth() throws GeneralSecurityException {
190         final String decryptedAuth = CryptoUtils.decrypt(vnfmAdapterAuth, msoKeyString);
191         final String[] auth = decryptedAuth.split(":");
192         return auth;
193     }
194
195 }