Feature uses features-parent as parent
[groupbasedpolicy.git] / renderers / opflex / src / main / java / org / opendaylight / groupbasedpolicy / renderer / opflex / OpflexRenderer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  * 
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.opflex;
10
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ScheduledExecutorService;
13
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.sal.binding.api.RpcProviderRegistry;
17 import org.opendaylight.groupbasedpolicy.renderer.opflex.lib.OpflexConnectionService;
18 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.AgentOvsMit;
19 import org.opendaylight.groupbasedpolicy.renderer.opflex.mit.MitLib;
20 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Renderer that uses OpFlex to implement an overlay network
27  * using Open vSwitch.
28  * 
29  * @author tbachman
30  */
31 public class OpflexRenderer implements AutoCloseable {
32
33     private static final Logger LOG = LoggerFactory.getLogger(OpflexRenderer.class);
34
35     private final PolicyResolver policyResolver;
36     private final EndpointManager endpointManager;
37     private final PolicyManager policyManager;
38     private final OpflexConnectionService connectionService;
39     private final MitLib mitLibrary;
40     private final ScheduledExecutorService executor;
41
42     ListenerRegistration<DataChangeListener> configReg;
43
44     public OpflexRenderer(DataBroker dataProvider, RpcProviderRegistry rpcRegistry) {
45         super();
46
47         int numCPU = Runtime.getRuntime().availableProcessors();
48         executor = Executors.newScheduledThreadPool(numCPU * 2);
49
50         mitLibrary = new MitLib();
51         MessageUtils.setOpflexLib(mitLibrary);
52         MessageUtils.init();
53         MessageUtils.setMit(new AgentOvsMit());
54
55         connectionService = new OpflexConnectionService(dataProvider, executor);
56
57         endpointManager = new EndpointManager(dataProvider, rpcRegistry, executor, connectionService, mitLibrary);
58         policyResolver = new PolicyResolver(dataProvider, executor);
59
60         policyManager = new PolicyManager(policyResolver, connectionService, executor, mitLibrary);
61
62         LOG.info("Initialized OpFlex renderer");
63     }
64
65     // *************
66     // AutoCloseable
67     // *************
68
69     @Override
70     public void close() throws Exception {
71         executor.shutdownNow();
72         if (configReg != null)
73             configReg.close();
74         if (policyResolver != null)
75             policyResolver.close();
76         if (policyManager != null)
77             policyManager.close();
78         if (connectionService != null)
79             connectionService.close();
80         if (endpointManager != null)
81             endpointManager.close();
82     }
83
84     // **************
85     // Implementation
86     // **************
87 }