Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-2016 Open Networking Laboratory |
| 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.cluster.impl; |
| 17 | |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 18 | import static org.onlab.util.Tools.groupedThreads; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.io.File; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 22 | import java.io.FileInputStream; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 23 | import java.io.IOException; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 24 | import java.net.URL; |
| 25 | import java.net.URLConnection; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 26 | import java.util.Set; |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 27 | import java.util.concurrent.Executors; |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 28 | import java.util.concurrent.ScheduledExecutorService; |
| 29 | import java.util.concurrent.TimeUnit; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 30 | import java.util.concurrent.atomic.AtomicReference; |
| 31 | |
| 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Reference; |
| 36 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 37 | import org.onlab.packet.IpAddress; |
| 38 | import org.onosproject.cluster.ClusterMetadata; |
| 39 | import org.onosproject.cluster.ClusterMetadataProvider; |
| 40 | import org.onosproject.cluster.ClusterMetadataProviderRegistry; |
| 41 | import org.onosproject.cluster.ClusterMetadataProviderService; |
| 42 | import org.onosproject.cluster.ControllerNode; |
| 43 | import org.onosproject.cluster.DefaultControllerNode; |
| 44 | import org.onosproject.cluster.DefaultPartition; |
| 45 | import org.onosproject.cluster.NodeId; |
| 46 | import org.onosproject.cluster.Partition; |
| 47 | import org.onosproject.cluster.PartitionId; |
| 48 | import org.onosproject.net.provider.ProviderId; |
| 49 | import org.onosproject.store.service.Versioned; |
| 50 | import org.slf4j.Logger; |
| 51 | |
| 52 | import com.fasterxml.jackson.core.JsonGenerator; |
| 53 | import com.fasterxml.jackson.core.JsonParser; |
| 54 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 55 | import com.fasterxml.jackson.databind.DeserializationContext; |
| 56 | import com.fasterxml.jackson.databind.JsonDeserializer; |
| 57 | import com.fasterxml.jackson.databind.JsonNode; |
| 58 | import com.fasterxml.jackson.databind.JsonSerializer; |
| 59 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 60 | import com.fasterxml.jackson.databind.SerializerProvider; |
| 61 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 62 | import com.google.common.base.Throwables; |
| 63 | import com.google.common.collect.Sets; |
| 64 | import com.google.common.io.Files; |
| 65 | |
| 66 | import static com.google.common.base.Preconditions.checkState; |
| 67 | |
| 68 | /** |
| 69 | * Provider of {@link ClusterMetadata cluster metadata} sourced from a local config file. |
| 70 | */ |
| 71 | @Component(immediate = true) |
| 72 | public class ConfigFileBasedClusterMetadataProvider implements ClusterMetadataProvider { |
| 73 | |
| 74 | private final Logger log = getLogger(getClass()); |
| 75 | |
| 76 | // constants for filed names (used in serialization) |
| 77 | private static final String ID = "id"; |
| 78 | private static final String PORT = "port"; |
| 79 | private static final String IP = "ip"; |
| 80 | |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 81 | private static final String CONFIG_DIR = "../config"; |
| 82 | private static final String CONFIG_FILE_NAME = "cluster.json"; |
| 83 | private static final File CONFIG_FILE = new File(CONFIG_DIR, CONFIG_FILE_NAME); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected ClusterMetadataProviderRegistry providerRegistry; |
| 87 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 88 | private static final ProviderId PROVIDER_ID = new ProviderId("file", "none"); |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 89 | private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>(); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 90 | private final ScheduledExecutorService configFileChangeDetector = |
| 91 | Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/cluster/metadata/config-watcher", "")); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 92 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 93 | private String metadataUrl; |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 94 | private ObjectMapper mapper; |
| 95 | private ClusterMetadataProviderService providerService; |
| 96 | |
| 97 | @Activate |
| 98 | public void activate() { |
| 99 | mapper = new ObjectMapper(); |
| 100 | SimpleModule module = new SimpleModule(); |
| 101 | module.addSerializer(NodeId.class, new NodeIdSerializer()); |
| 102 | module.addDeserializer(NodeId.class, new NodeIdDeserializer()); |
| 103 | module.addSerializer(ControllerNode.class, new ControllerNodeSerializer()); |
| 104 | module.addDeserializer(ControllerNode.class, new ControllerNodeDeserializer()); |
| 105 | module.addDeserializer(Partition.class, new PartitionDeserializer()); |
| 106 | module.addSerializer(PartitionId.class, new PartitionIdSerializer()); |
| 107 | module.addDeserializer(PartitionId.class, new PartitionIdDeserializer()); |
| 108 | mapper.registerModule(module); |
| 109 | providerService = providerRegistry.register(this); |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 110 | metadataUrl = System.getProperty("onos.cluster.metadata.uri", "file://" + CONFIG_DIR + "/" + CONFIG_FILE); |
| 111 | configFileChangeDetector.scheduleWithFixedDelay(() -> watchUrl(metadataUrl), 100, 500, TimeUnit.MILLISECONDS); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 112 | log.info("Started"); |
| 113 | } |
| 114 | |
| 115 | @Deactivate |
| 116 | public void deactivate() { |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 117 | configFileChangeDetector.shutdown(); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 118 | providerRegistry.unregister(this); |
| 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public ProviderId id() { |
| 124 | return PROVIDER_ID; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Versioned<ClusterMetadata> getClusterMetadata() { |
| 129 | checkState(isAvailable()); |
| 130 | synchronized (this) { |
| 131 | if (cachedMetadata.get() == null) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 132 | cachedMetadata.set(fetchMetadata(metadataUrl)); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 133 | } |
| 134 | return cachedMetadata.get(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void setClusterMetadata(ClusterMetadata metadata) { |
| 140 | try { |
| 141 | Files.createParentDirs(CONFIG_FILE); |
| 142 | mapper.writeValue(CONFIG_FILE, metadata); |
| 143 | providerService.clusterMetadataChanged(new Versioned<>(metadata, CONFIG_FILE.lastModified())); |
| 144 | } catch (IOException e) { |
| 145 | Throwables.propagate(e); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void addActivePartitionMember(PartitionId partitionId, NodeId nodeId) { |
| 151 | throw new UnsupportedOperationException(); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void removeActivePartitionMember(PartitionId partitionId, NodeId nodeId) { |
| 156 | throw new UnsupportedOperationException(); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public Set<NodeId> getActivePartitionMembers(PartitionId partitionId) { |
| 161 | throw new UnsupportedOperationException(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public boolean isAvailable() { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 166 | try { |
| 167 | URL url = new URL(metadataUrl); |
| 168 | if (url.getProtocol().equals("file")) { |
| 169 | File file = new File(metadataUrl.replaceFirst("file://", "")); |
| 170 | return file.exists(); |
| 171 | } else if (url.getProtocol().equals("http")) { |
| 172 | url.openStream(); |
| 173 | return true; |
| 174 | } else { |
| 175 | // Unsupported protocol |
| 176 | return false; |
| 177 | } |
| 178 | } catch (Exception e) { |
| 179 | return false; |
| 180 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 183 | private Versioned<ClusterMetadata> fetchMetadata(String metadataUrl) { |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 184 | try { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 185 | URL url = new URL(metadataUrl); |
| 186 | ClusterMetadata metadata = null; |
| 187 | long version = 0; |
| 188 | if (url.getProtocol().equals("file")) { |
| 189 | File file = new File(metadataUrl.replaceFirst("file://", "")); |
| 190 | version = file.lastModified(); |
| 191 | metadata = mapper.readValue(new FileInputStream(file), ClusterMetadata.class); |
| 192 | } else if (url.getProtocol().equals("http")) { |
| 193 | URLConnection conn = url.openConnection(); |
| 194 | version = conn.getLastModified(); |
| 195 | metadata = mapper.readValue(conn.getInputStream(), ClusterMetadata.class); |
| 196 | } |
| 197 | return new Versioned<>(new ClusterMetadata(PROVIDER_ID, |
| 198 | metadata.getName(), |
| 199 | Sets.newHashSet(metadata.getNodes()), |
| 200 | Sets.newHashSet(metadata.getPartitions())), |
| 201 | version); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 202 | } catch (IOException e) { |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 203 | throw Throwables.propagate(e); |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 204 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | private static class PartitionDeserializer extends JsonDeserializer<Partition> { |
| 208 | @Override |
| 209 | public Partition deserialize(JsonParser jp, DeserializationContext ctxt) |
| 210 | throws IOException, JsonProcessingException { |
| 211 | return jp.readValueAs(DefaultPartition.class); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private static class PartitionIdSerializer extends JsonSerializer<PartitionId> { |
| 216 | @Override |
| 217 | public void serialize(PartitionId partitionId, JsonGenerator jgen, SerializerProvider provider) |
| 218 | throws IOException, JsonProcessingException { |
| 219 | jgen.writeNumber(partitionId.asInt()); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | private class PartitionIdDeserializer extends JsonDeserializer<PartitionId> { |
| 224 | @Override |
| 225 | public PartitionId deserialize(JsonParser jp, DeserializationContext ctxt) |
| 226 | throws IOException, JsonProcessingException { |
| 227 | JsonNode node = jp.getCodec().readTree(jp); |
| 228 | return new PartitionId(node.asInt()); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | private static class ControllerNodeSerializer extends JsonSerializer<ControllerNode> { |
| 233 | @Override |
| 234 | public void serialize(ControllerNode node, JsonGenerator jgen, SerializerProvider provider) |
| 235 | throws IOException, JsonProcessingException { |
| 236 | jgen.writeStartObject(); |
| 237 | jgen.writeStringField(ID, node.id().toString()); |
| 238 | jgen.writeStringField(IP, node.ip().toString()); |
| 239 | jgen.writeNumberField(PORT, node.tcpPort()); |
| 240 | jgen.writeEndObject(); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private static class ControllerNodeDeserializer extends JsonDeserializer<ControllerNode> { |
| 245 | @Override |
| 246 | public ControllerNode deserialize(JsonParser jp, DeserializationContext ctxt) |
| 247 | throws IOException, JsonProcessingException { |
| 248 | JsonNode node = jp.getCodec().readTree(jp); |
| 249 | NodeId nodeId = new NodeId(node.get(ID).textValue()); |
| 250 | IpAddress ip = IpAddress.valueOf(node.get(IP).textValue()); |
| 251 | int port = node.get(PORT).asInt(); |
| 252 | return new DefaultControllerNode(nodeId, ip, port); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | private static class NodeIdSerializer extends JsonSerializer<NodeId> { |
| 257 | @Override |
| 258 | public void serialize(NodeId nodeId, JsonGenerator jgen, SerializerProvider provider) |
| 259 | throws IOException, JsonProcessingException { |
| 260 | jgen.writeString(nodeId.toString()); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | private class NodeIdDeserializer extends JsonDeserializer<NodeId> { |
| 265 | @Override |
| 266 | public NodeId deserialize(JsonParser jp, DeserializationContext ctxt) |
| 267 | throws IOException, JsonProcessingException { |
| 268 | JsonNode node = jp.getCodec().readTree(jp); |
| 269 | return new NodeId(node.asText()); |
| 270 | } |
| 271 | } |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 272 | |
| 273 | /** |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 274 | * Monitors the metadata url for any updates and notifies providerService accordingly. |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 275 | * @throws IOException |
| 276 | */ |
Madan Jampani | f172d40 | 2016-03-04 00:56:38 -0800 | [diff] [blame] | 277 | private void watchUrl(String metadataUrl) { |
| 278 | // TODO: We are merely polling the url. |
| 279 | // This can be easily addressed for files. For http urls we need to move to a push style protocol. |
| 280 | Versioned<ClusterMetadata> latestMetadata = fetchMetadata(metadataUrl); |
| 281 | if (cachedMetadata.get() != null && cachedMetadata.get().version() < latestMetadata.version()) { |
| 282 | cachedMetadata.set(latestMetadata); |
| 283 | providerService.clusterMetadataChanged(latestMetadata); |
Madan Jampani | 898fcca | 2016-02-26 12:42:08 -0800 | [diff] [blame] | 284 | } |
| 285 | } |
Madan Jampani | ad3c526 | 2016-01-20 00:50:17 -0800 | [diff] [blame] | 286 | } |