Fixing SO build
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-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.apache.commons.lang3.tuple.ImmutablePair;
33 import org.onap.so.adapters.vnfmadapter.common.VnfmAdapterUrlProvider;
34 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogServiceProvider;
35 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.BasicAuth;
36 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.NsdmSubscription;
37 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscription;
38 import org.onap.so.adapters.vnfmadapter.packagemanagement.model.InlineResponse201;
39 import org.onap.so.adapters.vnfmadapter.packagemanagement.model.PkgmSubscriptionRequest;
40 import org.onap.so.adapters.vnfmadapter.packagemanagement.model.SubscriptionsLinks;
41 import org.onap.so.adapters.vnfmadapter.packagemanagement.model.VnfPackagesLinksSelf;
42 import org.onap.so.adapters.vnfmadapter.packagemanagement.subscriptionmanagement.cache.PackageManagementCacheServiceProvider;
43 import org.onap.so.adapters.vnfmadapter.rest.exceptions.ConversionFailedException;
44 import org.onap.so.adapters.vnfmadapter.rest.exceptions.InternalServerErrorException;
45 import org.onap.so.adapters.vnfmadapter.rest.exceptions.SubscriptionNotFoundException;
46 import org.slf4j.Logger;
47 import org.springframework.beans.factory.annotation.Autowired;
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 VnfmAdapterUrlProvider vnfmAdapterUrlProvider;
65
66     @Autowired
67     public SubscriptionManager(final PackageManagementCacheServiceProvider packageManagementCacheServiceProvider,
68             final ConversionService conversionService, final EtsiCatalogServiceProvider etsiCatalogServiceProvider,
69             final VnfmAdapterUrlProvider vnfmAdapterUrlProvider) {
70         this.packageManagementCacheServiceProvider = packageManagementCacheServiceProvider;
71         this.conversionService = conversionService;
72         this.etsiCatalogServiceProvider = etsiCatalogServiceProvider;
73         this.vnfmAdapterUrlProvider = vnfmAdapterUrlProvider;
74     }
75
76     public Optional<InlineResponse201> createSubscription(final PkgmSubscriptionRequest pkgmSubscriptionRequest)
77             throws GeneralSecurityException {
78
79         final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest =
80                 buildEtsiCatalogManagerPkgmSubscriptionRequest(pkgmSubscriptionRequest);
81
82         final Optional<PkgmSubscription> optionalEtsiCatalogManagerSubscription =
83                 etsiCatalogServiceProvider.postSubscription(etsiCatalogManagerSubscriptionRequest);
84
85         if (optionalEtsiCatalogManagerSubscription.isPresent()) {
86             final PkgmSubscription etsiCatalogManagerSubscription = optionalEtsiCatalogManagerSubscription.get();
87             logger.debug("postPkgmSubscriptionRequest Response SubscriptionId: {}",
88                     Objects.requireNonNull(etsiCatalogManagerSubscription.getId()));
89             final String subscriptionId = etsiCatalogManagerSubscription.getId();
90
91             packageManagementCacheServiceProvider.addSubscription(subscriptionId, pkgmSubscriptionRequest);
92
93             final InlineResponse201 response = new InlineResponse201();
94             response.setId(subscriptionId);
95             response.setFilter(pkgmSubscriptionRequest.getFilter());
96             response.setCallbackUri(vnfmAdapterUrlProvider.getSubscriptionUriString(subscriptionId));
97             response.setLinks(new SubscriptionsLinks()
98                     .self(new VnfPackagesLinksSelf().href(getSubscriptionUri(subscriptionId).toString())));
99
100             return Optional.of(response);
101         }
102         throw new InternalServerErrorException(
103                 "Received empty response from POST to ETSI Catalog Manager Subscription Endpoint.");
104     }
105
106     public Optional<String> getSubscriptionId(final PkgmSubscriptionRequest pkgmSubscriptionRequest) {
107         return packageManagementCacheServiceProvider.getSubscriptionId(pkgmSubscriptionRequest);
108     }
109
110     public Optional<InlineResponse201> getSubscription(final String subscriptionId) {
111
112         logger.debug("Checking if subscrition with id: {} exists in ETSI Catalog Manager", subscriptionId);
113         final Optional<NsdmSubscription> etsiCatalogSubscriptionOption =
114                 etsiCatalogServiceProvider.getSubscription(subscriptionId);
115
116         if (!etsiCatalogSubscriptionOption.isPresent()) {
117             logger.debug("Unable to find subscription in ETSI Catalog Manager using id: {}", subscriptionId);
118             if (packageManagementCacheServiceProvider.getSubscription(subscriptionId).isPresent()) {
119                 logger.debug("will remove subcription with id: {} from local cache", subscriptionId);
120                 packageManagementCacheServiceProvider.deleteSubscription(subscriptionId);
121             }
122         }
123
124         final Optional<PkgmSubscriptionRequest> optional =
125                 packageManagementCacheServiceProvider.getSubscription(subscriptionId);
126         if (optional.isPresent()) {
127             final PkgmSubscriptionRequest subscription = optional.get();
128             return Optional.of(getInlineResponse2002(subscriptionId, subscription));
129         }
130         return Optional.empty();
131     }
132
133     public List<InlineResponse201> getSubscriptions() {
134         final Map<String, PkgmSubscriptionRequest> subscriptions =
135                 packageManagementCacheServiceProvider.getSubscriptions();
136         final List<InlineResponse201> response = new ArrayList<>();
137         subscriptions.forEach((key, value) -> {
138             final Optional<InlineResponse201> optional = getSubscription(key);
139             if (optional.isPresent()) {
140                 response.add(optional.get());
141             }
142         });
143         return response;
144     }
145
146     public boolean deleteSubscription(final String subscriptionId) {
147         if (packageManagementCacheServiceProvider.getSubscription(subscriptionId).isPresent()) {
148             try {
149                 if (etsiCatalogServiceProvider.deleteSubscription(subscriptionId)) {
150                     return packageManagementCacheServiceProvider.deleteSubscription(subscriptionId);
151                 }
152             } catch (final SubscriptionNotFoundException subscriptionNotFoundException) {
153                 logger.error(
154                         "Unable to find subscription in ETSI Catalog Manager using id: {} will delete it from local cache",
155                         subscriptionId);
156                 return packageManagementCacheServiceProvider.deleteSubscription(subscriptionId);
157             }
158         }
159         return false;
160     }
161
162     public URI getSubscriptionUri(final String subscriptionId) {
163         return vnfmAdapterUrlProvider.getSubscriptionUri(subscriptionId);
164     }
165
166     public Optional<PkgmSubscriptionRequest> getSubscriptionRequest(final String subscriptionId) {
167         return packageManagementCacheServiceProvider.getSubscription(subscriptionId);
168     }
169
170     private InlineResponse201 getInlineResponse2002(final String id, final PkgmSubscriptionRequest subscription) {
171         return new InlineResponse201().id(id).filter(subscription.getFilter())
172                 .callbackUri(subscription.getCallbackUri());
173     }
174
175     private org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest buildEtsiCatalogManagerPkgmSubscriptionRequest(
176             final PkgmSubscriptionRequest pkgmSubscriptionRequest) throws GeneralSecurityException {
177
178         final org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest etsiCatalogManagerSubscriptionRequest;
179         try {
180             etsiCatalogManagerSubscriptionRequest = conversionService.convert(pkgmSubscriptionRequest,
181                     org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.PkgmSubscriptionRequest.class);
182         } catch (final org.springframework.core.convert.ConversionException conversionException) {
183             logger.error(conversionException.getMessage());
184             throw new ConversionFailedException(
185                     "Could not convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest");
186         } catch (final Exception exception) {
187             logger.error(exception.getMessage());
188             throw new InternalServerErrorException(
189                     "Could not convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest");
190         }
191
192         if (etsiCatalogManagerSubscriptionRequest != null) {
193             etsiCatalogManagerSubscriptionRequest
194                     .setCallbackUri(vnfmAdapterUrlProvider.getEtsiSubscriptionNotificationBaseUrl());
195
196             final ImmutablePair<String, String> immutablePair = vnfmAdapterUrlProvider.getDecryptAuth();
197             if (!immutablePair.equals(ImmutablePair.nullPair())) {
198                 etsiCatalogManagerSubscriptionRequest.setAuthentication(
199                         new org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.SubscriptionAuthentication()
200                                 .addAuthTypeItem(BASIC).paramsBasic(new BasicAuth().userName(immutablePair.getLeft())
201                                         .password(immutablePair.getRight())));
202             }
203             return etsiCatalogManagerSubscriptionRequest;
204         }
205         throw new ConversionFailedException(
206                 "Failed to convert Sol003 PkgmSubscriptionRequest to ETSI-Catalog Manager PkgmSubscriptionRequest");
207     }
208
209 }