Switched from Dropwizard to Springboot
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / resources / DmaapConfigurationService.java
1 /*
2  * Copyright 2017-2022 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.engine.resources;
18
19 import io.swagger.annotations.ApiOperation;
20 import io.swagger.annotations.ApiParam;
21 import jakarta.ws.rs.Path;
22 import jakarta.ws.rs.core.MediaType;
23 import lombok.extern.slf4j.Slf4j;
24 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
25 import org.onap.holmes.common.dcae.entity.SecurityInfo;
26 import org.onap.holmes.common.utils.SpringContextUtil;
27 import org.onap.holmes.dsa.dmaappolling.Subscriber;
28 import org.onap.holmes.engine.dmaap.SubscriberAction;
29 import org.onap.holmes.engine.request.DmaapConfigRequest;
30 import org.springframework.web.bind.annotation.*;
31
32 @Slf4j
33 @RestController
34 @RequestMapping("/dmaap")
35 public class DmaapConfigurationService {
36     @ApiOperation(value = "Subscribe to a new topic. "
37             + "If the topic already exists, it is replaced with the new configuration.")
38     @RequestMapping(value = "/sub", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON)
39     public String addSubInfo(
40             @ApiParam(value = "A JSON object with the fields named <b>name</b>"
41                     + " and <b>url</b>. Both fields are required.")
42             @RequestBody DmaapConfigRequest config) {
43         String url = config.getUrl();
44         if (url.startsWith("http://") || url.startsWith("https://")) {
45             Subscriber subscriber = new Subscriber();
46             subscriber.setTopic(config.getName());
47             subscriber.setUrl(url);
48
49             SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class);
50             subscriberAction.removeSubscriber(subscriber);
51             subscriberAction.addSubscriber(subscriber);
52
53             log.info("New configurations applied. Topic Name: " + config.getName() + ", URL: " + url + ".");
54
55             return "{\"message\": \"Succeeded!\", \"topic\": \"" + config.getName() + "\"}";
56         }
57         return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}";
58     }
59
60     @Path("/sub/{topic}")
61     @ApiOperation(value = "Unsubscribe a topic from DMaaP.")
62     @RequestMapping(value = "/sub/{topic}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON)
63     public String removeSubInfo(@PathVariable("topic") String topic) {
64         Subscriber subscriber = new Subscriber();
65         subscriber.setTopic(topic);
66
67         SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class);
68         subscriberAction.removeSubscriber(subscriber);
69
70         return "{\"message\": \"Topic unsubscribed.\"}";
71     }
72
73     @ApiOperation(value = "Add/Update a publishing topic. "
74             + "If the topic already exists, it is replaced with the new configuration.")
75     @RequestMapping(value = "/pub", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON)
76     public String updatePubInfo(
77             @ApiParam(value = "A JSON object with the fields named <b>name</b>"
78                     + " and <b>url</b>. Both fields are required.")
79             @RequestBody DmaapConfigRequest config) {
80         String url = config.getUrl();
81         if (url.startsWith("http://") || url.startsWith("https://")) {
82             SecurityInfo securityInfo = new SecurityInfo();
83             SecurityInfo.DmaapInfo dmaapInfo = new SecurityInfo().new DmaapInfo();
84             dmaapInfo.setTopicUrl(config.getUrl());
85             securityInfo.setDmaapInfo(dmaapInfo);
86             DcaeConfigurationsCache.addPubSecInfo(config.getName(), securityInfo);
87             return "{\"message\": \"Succeeded!\", \"topic\": \"" + config.getName() + "\"}";
88         }
89         return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}";
90     }
91 }