2c6b8f246f9309d40a7981458f4e70736f5cc737
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / vnfm-simulator / vnfm-service / src / main / java / org / onap / so / svnfm / simulator / controller / SubscriptionNotificationController.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.svnfm.simulator.controller;
22
23 import javax.ws.rs.core.MediaType;
24 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse201;
25 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.PkgmSubscriptionRequest;
26 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthentication;
27 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthentication.AuthTypeEnum;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.SubscriptionsAuthenticationParamsBasic;
29 import org.onap.so.svnfm.simulator.constants.Constant;
30 import org.onap.so.svnfm.simulator.services.SubscriptionManager;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PostMapping;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RestController;
41
42 /**
43  * @author Eoin Hanan (eoin.hanan@est.tech)
44  * @author Andrew Lamb (andrew.a.lamb@est.tech)
45  *
46  */
47 @RestController
48 @RequestMapping(path = Constant.PACKAGE_MANAGEMENT_BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
49 public class SubscriptionNotificationController {
50     @Autowired
51     private SubscriptionManager subscriptionManager;
52
53     private static final Logger logger = LoggerFactory.getLogger(SubscriptionNotificationController.class);
54
55     @Value("${spring.security.usercredentials[0].username}")
56     private String username;
57     @Value("${spring.security.usercredentials[0].password}")
58     private String password;
59
60     @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
61     public ResponseEntity<Void> testEndpoint() {
62         logger.info("Testing SubscriptionNotification Endpoint");
63         return ResponseEntity.noContent().build();
64     }
65
66     /**
67      * Send subscription request
68      *
69      * @param pkgmSubscriptionRequest The subscription request
70      * @return
71      */
72     @PostMapping(value = Constant.SUBSCRIPTION_ENDPOINT, produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
73     public ResponseEntity<?> subscribeForNotifications(@RequestBody final PkgmSubscriptionRequest pkgmSubscriptionRequest){
74         logger.info("Vnf Package Subscription Request received:\n{}", pkgmSubscriptionRequest);
75
76         if(pkgmSubscriptionRequest.getCallbackUri()==null){
77             logger.info("Subscription Request does not include call back URI ");
78             pkgmSubscriptionRequest.setCallbackUri(Constant.PACKAGE_MANAGEMENT_BASE_URL + Constant.NOTIFICATION_ENDPOINT);
79         }
80
81         if(pkgmSubscriptionRequest.getAuthentication()==null) {
82             pkgmSubscriptionRequest.setAuthentication(new SubscriptionsAuthentication()
83                 .addAuthTypeItem(AuthTypeEnum.BASIC)
84                 .paramsBasic(new SubscriptionsAuthenticationParamsBasic().userName(username).password(password)));
85
86         }
87         logger.info("Final Request being sent:\n{}", pkgmSubscriptionRequest);
88
89         final InlineResponse201 response = subscriptionManager.createSubscription(pkgmSubscriptionRequest);
90         logger.info("Response is:\n{}", response);
91
92         return ResponseEntity.ok().body(response);
93     }
94
95     /**
96      * Endpoint to receive VNF package notifications
97      *
98      * @param notification The notification received
99      * @return
100      */
101     @PostMapping(value = Constant.NOTIFICATION_ENDPOINT, produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
102     public ResponseEntity<?> postVnfPackageNotification(@RequestBody Object notification){
103         logger.info("Vnf Notification received:\n{}", notification);
104         return ResponseEntity.noContent().build();
105     }
106
107 }