blob: 8f34ec47e1f312a603e45501312371f97cb5ba1c [file] [log] [blame]
Madan Jampaniad3c5262016-01-20 00:50:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampaniad3c5262016-01-20 00:50:17 -08003 *
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.cluster.impl;
17
Madan Jampani898fcca2016-02-26 12:42:08 -080018import static org.onlab.util.Tools.groupedThreads;
Madan Jampaniad3c5262016-01-20 00:50:17 -080019import static org.slf4j.LoggerFactory.getLogger;
20
21import java.io.File;
Madan Jampanif172d402016-03-04 00:56:38 -080022import java.io.FileInputStream;
Madan Jampaniad3c5262016-01-20 00:50:17 -080023import java.io.IOException;
Madan Jampanif172d402016-03-04 00:56:38 -080024import java.net.URL;
25import java.net.URLConnection;
Madan Jampaniad3c5262016-01-20 00:50:17 -080026import java.util.Set;
Madan Jampani898fcca2016-02-26 12:42:08 -080027import java.util.concurrent.Executors;
Madan Jampanif172d402016-03-04 00:56:38 -080028import java.util.concurrent.ScheduledExecutorService;
29import java.util.concurrent.TimeUnit;
Madan Jampaniad3c5262016-01-20 00:50:17 -080030import java.util.concurrent.atomic.AtomicReference;
31
32import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Component;
34import org.apache.felix.scr.annotations.Deactivate;
35import org.apache.felix.scr.annotations.Reference;
36import org.apache.felix.scr.annotations.ReferenceCardinality;
37import org.onlab.packet.IpAddress;
38import org.onosproject.cluster.ClusterMetadata;
39import org.onosproject.cluster.ClusterMetadataProvider;
40import org.onosproject.cluster.ClusterMetadataProviderRegistry;
41import org.onosproject.cluster.ClusterMetadataProviderService;
42import org.onosproject.cluster.ControllerNode;
43import org.onosproject.cluster.DefaultControllerNode;
44import org.onosproject.cluster.DefaultPartition;
45import org.onosproject.cluster.NodeId;
46import org.onosproject.cluster.Partition;
47import org.onosproject.cluster.PartitionId;
48import org.onosproject.net.provider.ProviderId;
49import org.onosproject.store.service.Versioned;
50import org.slf4j.Logger;
51
52import com.fasterxml.jackson.core.JsonGenerator;
53import com.fasterxml.jackson.core.JsonParser;
54import com.fasterxml.jackson.core.JsonProcessingException;
55import com.fasterxml.jackson.databind.DeserializationContext;
56import com.fasterxml.jackson.databind.JsonDeserializer;
57import com.fasterxml.jackson.databind.JsonNode;
58import com.fasterxml.jackson.databind.JsonSerializer;
59import com.fasterxml.jackson.databind.ObjectMapper;
60import com.fasterxml.jackson.databind.SerializerProvider;
61import com.fasterxml.jackson.databind.module.SimpleModule;
62import com.google.common.base.Throwables;
63import com.google.common.collect.Sets;
64import com.google.common.io.Files;
65
66import 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)
72public 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 Jampani898fcca2016-02-26 12:42:08 -080081 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 Jampaniad3c5262016-01-20 00:50:17 -080084
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected ClusterMetadataProviderRegistry providerRegistry;
87
Madan Jampanif172d402016-03-04 00:56:38 -080088 private static final ProviderId PROVIDER_ID = new ProviderId("file", "none");
Madan Jampani898fcca2016-02-26 12:42:08 -080089 private final AtomicReference<Versioned<ClusterMetadata>> cachedMetadata = new AtomicReference<>();
Madan Jampanif172d402016-03-04 00:56:38 -080090 private final ScheduledExecutorService configFileChangeDetector =
91 Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/cluster/metadata/config-watcher", ""));
Madan Jampaniad3c5262016-01-20 00:50:17 -080092
Madan Jampanif172d402016-03-04 00:56:38 -080093 private String metadataUrl;
Madan Jampaniad3c5262016-01-20 00:50:17 -080094 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 Jampanif172d402016-03-04 00:56:38 -0800110 metadataUrl = System.getProperty("onos.cluster.metadata.uri", "file://" + CONFIG_DIR + "/" + CONFIG_FILE);
111 configFileChangeDetector.scheduleWithFixedDelay(() -> watchUrl(metadataUrl), 100, 500, TimeUnit.MILLISECONDS);
Madan Jampaniad3c5262016-01-20 00:50:17 -0800112 log.info("Started");
113 }
114
115 @Deactivate
116 public void deactivate() {
Madan Jampani898fcca2016-02-26 12:42:08 -0800117 configFileChangeDetector.shutdown();
Madan Jampaniad3c5262016-01-20 00:50:17 -0800118 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 Jampanif172d402016-03-04 00:56:38 -0800132 cachedMetadata.set(fetchMetadata(metadataUrl));
Madan Jampaniad3c5262016-01-20 00:50:17 -0800133 }
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 Jampanif172d402016-03-04 00:56:38 -0800166 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 Jampaniad3c5262016-01-20 00:50:17 -0800181 }
182
Madan Jampanif172d402016-03-04 00:56:38 -0800183 private Versioned<ClusterMetadata> fetchMetadata(String metadataUrl) {
Madan Jampaniad3c5262016-01-20 00:50:17 -0800184 try {
Madan Jampanif172d402016-03-04 00:56:38 -0800185 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 Jampaniad3c5262016-01-20 00:50:17 -0800202 } catch (IOException e) {
Madan Jampanif172d402016-03-04 00:56:38 -0800203 throw Throwables.propagate(e);
Madan Jampaniad3c5262016-01-20 00:50:17 -0800204 }
Madan Jampaniad3c5262016-01-20 00:50:17 -0800205 }
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 Jampani898fcca2016-02-26 12:42:08 -0800272
273 /**
Madan Jampanif172d402016-03-04 00:56:38 -0800274 * Monitors the metadata url for any updates and notifies providerService accordingly.
Madan Jampani898fcca2016-02-26 12:42:08 -0800275 */
Madan Jampanif172d402016-03-04 00:56:38 -0800276 private void watchUrl(String metadataUrl) {
277 // TODO: We are merely polling the url.
278 // This can be easily addressed for files. For http urls we need to move to a push style protocol.
279 Versioned<ClusterMetadata> latestMetadata = fetchMetadata(metadataUrl);
280 if (cachedMetadata.get() != null && cachedMetadata.get().version() < latestMetadata.version()) {
281 cachedMetadata.set(latestMetadata);
282 providerService.clusterMetadataChanged(latestMetadata);
Madan Jampani898fcca2016-02-26 12:42:08 -0800283 }
284 }
Madan Jampaniad3c5262016-01-20 00:50:17 -0800285}