blob: 6f5710b4090b1a8a5bd8c00b67b7e95600ec73ca [file] [log] [blame]
Jian Li6803ccd2018-06-08 09:26:09 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacktelemetry.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Service;
22import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService;
23import org.onosproject.openstacktelemetry.api.config.RestTelemetryConfig;
24import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.client.Client;
29import javax.ws.rs.client.ClientBuilder;
30import javax.ws.rs.client.Entity;
31import javax.ws.rs.client.WebTarget;
32import javax.ws.rs.core.Response;
33
34/**
35 * REST telemetry manager.
36 */
37@Component(immediate = true)
38@Service
39public class RestTelemetryManager implements RestTelemetryAdminService {
40
41 private final Logger log = LoggerFactory.getLogger(getClass());
42
43 private static final String PROTOCOL = "http";
44 private static final String POST_METHOD = "POST";
45 private static final String GET_METHOD = "GET";
46
47 private WebTarget target = null;
48 private RestTelemetryConfig restConfig = null;
49
50 @Activate
51 protected void activate() {
52 log.info("Started");
53 }
54
55 @Deactivate
56 protected void deactivate() {
57 stop();
58 log.info("Stopped");
59 }
60
61 @Override
62 public void start(TelemetryConfig config) {
63 if (target != null) {
64 log.info("REST producer has already been started");
65 return;
66 }
67
68 restConfig = (RestTelemetryConfig) config;
69
70 StringBuilder restServerBuilder = new StringBuilder();
71 restServerBuilder.append(PROTOCOL);
72 restServerBuilder.append(":");
73 restServerBuilder.append("//");
74 restServerBuilder.append(restConfig.address());
75 restServerBuilder.append(":");
76 restServerBuilder.append(restConfig.port());
77 restServerBuilder.append("/");
78
79 Client client = ClientBuilder.newBuilder().build();
80
81 target = client.target(restServerBuilder.toString()).path(restConfig.endpoint());
82
83 log.info("REST producer has Started");
84 }
85
86 @Override
87 public void stop() {
88 if (target != null) {
89 target = null;
90 }
91
92 log.info("REST producer has Stopped");
93 }
94
95 @Override
96 public void restart(TelemetryConfig config) {
97 stop();
98 start(config);
99 }
100
101 @Override
102 public Response publish(String endpoint, String method, String record) {
103 // TODO: need to find a way to invoke REST endpoint using target
104 return null;
105 }
106
107 @Override
108 public Response publish(String method, String record) {
109 switch (method) {
110 case POST_METHOD:
111 return target.request(restConfig.requestMediaType())
112 .post(Entity.json(record));
113 case GET_METHOD:
114 return target.request(restConfig.requestMediaType()).get();
115 default:
116 return null;
117 }
118 }
119
120 @Override
121 public Response publish(String record) {
122 switch (restConfig.method()) {
123 case POST_METHOD:
124 return target.request(restConfig.requestMediaType())
125 .post(Entity.json(record));
126 case GET_METHOD:
127 return target.request(restConfig.requestMediaType()).get();
128 default:
129 return null;
130 }
131 }
132}