2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay;
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ScheduledExecutorService;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
21 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayConfig;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.common.base.Optional;
31 import com.google.common.util.concurrent.FutureCallback;
32 import com.google.common.util.concurrent.Futures;
33 import com.google.common.util.concurrent.ListenableFuture;
36 * Renderer that uses OpenFlow and OVSDB to implement an overlay network
40 public class OFOverlayRenderer implements AutoCloseable, DataChangeListener {
41 private static final Logger LOG =
42 LoggerFactory.getLogger(OFOverlayRenderer.class);
44 private final Uuid ID = new Uuid("934cd993-7bcc-46cf-b150-64fabc92b4e0");
45 private final DataBroker dataBroker;
46 private final PolicyResolver policyResolver;
47 private final SwitchManager switchManager;
48 private final EndpointManager endpointManager;
49 private final PolicyManager policyManager;
51 private final ScheduledExecutorService executor;
53 private static final InstanceIdentifier<OfOverlayConfig> configIid =
54 InstanceIdentifier.builder(OfOverlayConfig.class).build();
56 private OfOverlayConfig config;
57 ListenerRegistration<DataChangeListener> configReg;
59 public OFOverlayRenderer(DataBroker dataProvider,
60 RpcProviderRegistry rpcRegistry) {
62 this.dataBroker = dataProvider;
64 int numCPU = Runtime.getRuntime().availableProcessors();
65 //TODO: Consider moving to groupbasedpolicy-ofoverlay-config so as to be user configurable in distribution.
66 executor = Executors.newScheduledThreadPool(numCPU * 2);
68 switchManager = new SwitchManager(dataProvider);
69 endpointManager = new EndpointManager(dataProvider, rpcRegistry,
70 executor, switchManager);
71 policyResolver = new PolicyResolver(dataProvider, executor);
73 policyManager = new PolicyManager(dataProvider,
81 dataProvider.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
84 DataChangeScope.SUBTREE);
86 LOG.info("Initialized OFOverlay renderer");
95 public void close() throws Exception {
96 executor.shutdownNow();
97 if (configReg != null) configReg.close();
98 if (policyResolver != null) policyResolver.close();
99 if (switchManager != null) switchManager.close();
100 if (endpointManager != null) endpointManager.close();
103 // ******************
104 // DataChangeListener
105 // ******************
108 public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>,
109 DataObject> change) {
117 private void readConfig() {
118 ListenableFuture<Optional<OfOverlayConfig>> dao =
119 dataBroker.newReadOnlyTransaction()
120 .read(LogicalDatastoreType.CONFIGURATION, configIid);
121 Futures.addCallback(dao, new FutureCallback<Optional<OfOverlayConfig>>() {
123 public void onSuccess(final Optional<OfOverlayConfig> result) {
124 if (!result.isPresent()) return;
125 if (result.get() instanceof OfOverlayConfig) {
126 config = result.get();
132 public void onFailure(Throwable t) {
133 LOG.error("Failed to read configuration", t);
138 private void applyConfig() {
139 switchManager.setEncapsulationFormat(config.getEncapsulationFormat());
140 endpointManager.setLearningMode(config.getLearningMode());
141 policyManager.setLearningMode(config.getLearningMode());