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