2 * Copyright 2017 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 javax.servlet.http.HttpServletRequest;
22 import javax.ws.rs.DELETE;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.Produces;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.MediaType;
29 import lombok.extern.slf4j.Slf4j;
30 import org.jvnet.hk2.annotations.Service;
31 import org.onap.holmes.common.dcae.DcaeConfigurationsCache;
32 import org.onap.holmes.common.dcae.entity.SecurityInfo;
33 import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder;
34 import org.onap.holmes.dsa.dmaappolling.Subscriber;
35 import org.onap.holmes.engine.dmaap.SubscriberAction;
36 import org.onap.holmes.engine.request.DmaapConfigRequest;
40 //@Api(tags = {"DMaaP Configurations"})
42 public class DmaapConfigurationService {
44 @Produces(MediaType.APPLICATION_JSON)
45 @ApiOperation(value = "Subscribe to a new topic. "
46 + "If the topic already exists, it is replaced with the new configuration.")
48 public String addSubInfo(
49 @ApiParam (value = "A JSON object with the fields named <b>name</b>"
50 + " and <b>url</b>. Both fields are required.") DmaapConfigRequest config,
51 @Context HttpServletRequest request){
52 String url = config.getUrl();
53 if (url.startsWith("http://")) {
54 Subscriber subscriber = new Subscriber();
55 subscriber.setTopic(config.getName());
56 subscriber.setUrl(url);
58 SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
59 .getService(SubscriberAction.class);
60 subscriberAction.removeSubscriber(subscriber);
61 subscriberAction.addSubscriber(subscriber);
63 log.info("New configurations applied. Topic Name: " + config.getName() + ", URL: " + url + ".");
65 return "{\"message\": \"Succeeded!\", \"topic\": \"" + config.getName() + "\"}";
67 return "{\"message\": \"Only the HTTP protocol is supported!\"}";
72 @ApiOperation(value = "Unsubscribe a topic from DMaaP.")
73 @Produces(MediaType.APPLICATION_JSON)
74 public String removeSubInfo(@PathParam("topic") String topic){
75 Subscriber subscriber = new Subscriber();
76 subscriber.setTopic(topic);
78 SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
79 .getService(SubscriberAction.class);
80 subscriberAction.removeSubscriber(subscriber);
82 return "{\"message\": \"Topic unsubscribed.\"}";
86 @Produces(MediaType.APPLICATION_JSON)
88 @ApiOperation(value = "Add/Update a publishing topic. "
89 + "If the topic already exists, it is replaced with the new configuration.")
90 public String updatePubInfo(
91 @ApiParam (value = "A JSON object with the fields named <b>name</b>"
92 + " and <b>url</b>. Both fields are required.") DmaapConfigRequest config,
93 @Context HttpServletRequest request){
94 String url = config.getUrl();
95 if (url.startsWith("http://")) {
96 SecurityInfo securityInfo = new SecurityInfo();
97 SecurityInfo.DmaapInfo dmaapInfo = new SecurityInfo().new DmaapInfo();
98 dmaapInfo.setTopicUrl(config.getUrl());
99 securityInfo.setDmaapInfo(dmaapInfo);
100 DcaeConfigurationsCache.addPubSecInfo(config.getName(), securityInfo);
101 return "{\"message\": \"Succeeded!\", \"topic\": \"" + config.getName() + "\"}";
103 return "{\"message\": \"Only the HTTP protocol is supported!\"}";