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