548b9b297030f46729f7b4312f1c40814d2d6e33
[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.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;
37
38 @Service
39 @Slf4j
40 //@Api(tags = {"DMaaP Configurations"})
41 @Path("/dmaap")
42 public class DmaapConfigurationService {
43     @PUT
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.")
47     @Path("/sub")
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://") || url.startsWith("https://")) {
54             Subscriber subscriber = new Subscriber();
55             subscriber.setTopic(config.getName());
56             subscriber.setUrl(url);
57
58             SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
59                     .getService(SubscriberAction.class);
60             subscriberAction.removeSubscriber(subscriber);
61             subscriberAction.addSubscriber(subscriber);
62
63             log.info("New configurations applied. Topic Name: " + config.getName() + ", URL: " + url + ".");
64
65             return "{\"message\": \"Succeeded!\", \"topic\": \"" + config.getName() + "\"}";
66         }
67         return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}";
68     }
69
70     @DELETE
71     @Path("/sub/{topic}")
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);
77
78         SubscriberAction subscriberAction = ServiceLocatorHolder.getLocator()
79                 .getService(SubscriberAction.class);
80         subscriberAction.removeSubscriber(subscriber);
81
82         return "{\"message\": \"Topic unsubscribed.\"}";
83     }
84
85     @PUT
86     @Produces(MediaType.APPLICATION_JSON)
87     @Path("/pub")
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://") || url.startsWith("https://")) {
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() + "\"}";
102         }
103         return "{\"message\": \"Only the HTTP or HTTPS protocol is supported!\"}";
104     }
105 }