2 * Copyright 2017-2022 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.engine.resources;
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.*;
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);
49 SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class);
50 subscriberAction.removeSubscriber(subscriber);
51 subscriberAction.addSubscriber(subscriber);
53 log.info("New configurations applied. Topic Name: " + config.getName() + ", URL: " + url + ".");
55 return "{\"message\": \"Succeeded!\", \"topic\": \"" + config.getName() + "\"}";
57 return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}";
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);
67 SubscriberAction subscriberAction = SpringContextUtil.getBean(SubscriberAction.class);
68 subscriberAction.removeSubscriber(subscriber);
70 return "{\"message\": \"Topic unsubscribed.\"}";
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() + "\"}";
89 return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}";