Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 18 | import org.onosproject.openstacktelemetry.api.OpenstackTelemetryService; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 19 | import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService; |
| 20 | import org.onosproject.openstacktelemetry.api.config.RestTelemetryConfig; |
| 21 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.osgi.service.component.annotations.Activate; |
| 23 | import org.osgi.service.component.annotations.Component; |
| 24 | import org.osgi.service.component.annotations.Deactivate; |
| 25 | import org.osgi.service.component.annotations.Reference; |
| 26 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import javax.ws.rs.client.Client; |
| 31 | import javax.ws.rs.client.ClientBuilder; |
| 32 | import javax.ws.rs.client.Entity; |
| 33 | import javax.ws.rs.client.WebTarget; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | |
| 36 | /** |
| 37 | * REST telemetry manager. |
| 38 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Component(immediate = true, service = RestTelemetryAdminService.class) |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 40 | public class RestTelemetryManager implements RestTelemetryAdminService { |
| 41 | |
| 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | |
| 44 | private static final String PROTOCOL = "http"; |
| 45 | private static final String POST_METHOD = "POST"; |
| 46 | private static final String GET_METHOD = "GET"; |
| 47 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 49 | protected OpenstackTelemetryService openstackTelemetryService; |
| 50 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 51 | private WebTarget target = null; |
| 52 | private RestTelemetryConfig restConfig = null; |
| 53 | |
| 54 | @Activate |
| 55 | protected void activate() { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 56 | |
| 57 | openstackTelemetryService.addTelemetryService(this); |
| 58 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 59 | log.info("Started"); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | protected void deactivate() { |
| 64 | stop(); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 65 | |
| 66 | openstackTelemetryService.removeTelemetryService(this); |
| 67 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 68 | log.info("Stopped"); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void start(TelemetryConfig config) { |
| 73 | if (target != null) { |
| 74 | log.info("REST producer has already been started"); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | restConfig = (RestTelemetryConfig) config; |
| 79 | |
| 80 | StringBuilder restServerBuilder = new StringBuilder(); |
| 81 | restServerBuilder.append(PROTOCOL); |
| 82 | restServerBuilder.append(":"); |
| 83 | restServerBuilder.append("//"); |
| 84 | restServerBuilder.append(restConfig.address()); |
| 85 | restServerBuilder.append(":"); |
| 86 | restServerBuilder.append(restConfig.port()); |
| 87 | restServerBuilder.append("/"); |
| 88 | |
| 89 | Client client = ClientBuilder.newBuilder().build(); |
| 90 | |
| 91 | target = client.target(restServerBuilder.toString()).path(restConfig.endpoint()); |
| 92 | |
| 93 | log.info("REST producer has Started"); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void stop() { |
| 98 | if (target != null) { |
| 99 | target = null; |
| 100 | } |
| 101 | |
| 102 | log.info("REST producer has Stopped"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void restart(TelemetryConfig config) { |
| 107 | stop(); |
| 108 | start(config); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public Response publish(String endpoint, String method, String record) { |
| 113 | // TODO: need to find a way to invoke REST endpoint using target |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Response publish(String method, String record) { |
| 119 | switch (method) { |
| 120 | case POST_METHOD: |
| 121 | return target.request(restConfig.requestMediaType()) |
| 122 | .post(Entity.json(record)); |
| 123 | case GET_METHOD: |
| 124 | return target.request(restConfig.requestMediaType()).get(); |
| 125 | default: |
| 126 | return null; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public Response publish(String record) { |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 132 | |
| 133 | if (target == null) { |
Jian Li | 6c92b3c | 2018-08-03 11:26:55 +0900 | [diff] [blame] | 134 | log.debug("REST telemetry service has not been enabled!"); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 135 | return null; |
| 136 | } |
| 137 | |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 138 | switch (restConfig.method()) { |
| 139 | case POST_METHOD: |
| 140 | return target.request(restConfig.requestMediaType()) |
| 141 | .post(Entity.json(record)); |
| 142 | case GET_METHOD: |
| 143 | return target.request(restConfig.requestMediaType()).get(); |
| 144 | default: |
| 145 | return null; |
| 146 | } |
| 147 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | public boolean isRunning() { |
| 151 | return target != null; |
| 152 | } |
Jian Li | 6803ccd | 2018-06-08 09:26:09 +0900 | [diff] [blame] | 153 | } |