Merge "Turn samples/pom.xml into an aggregator"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleManagerImpl.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.role;
10
11 import io.netty.util.HashedWheelTimer;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import java.util.concurrent.ExecutorService;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
19 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
20 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
21 import org.opendaylight.openflowplugin.impl.services.sal.SalRoleServiceImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
23
24 public class RoleManagerImpl implements RoleManager {
25     // Timeout after what we will give up on waiting for master role
26     private static final long CHECK_ROLE_MASTER_TIMEOUT = 20000;
27
28     private final ConcurrentMap<DeviceInfo, RoleContext> contexts = new ConcurrentHashMap<>();
29     private final HashedWheelTimer timer;
30     private final OpenflowProviderConfig config;
31     private final ExecutorService executorService;
32
33     public RoleManagerImpl(final HashedWheelTimer timer,
34                            final OpenflowProviderConfig config,
35                            final ExecutorService executorService) {
36         this.timer = timer;
37         this.config = config;
38         this.executorService = executorService;
39     }
40
41     @Override
42     public RoleContext createContext(@NonNull final DeviceContext deviceContext) {
43         final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
44         final RoleContextImpl roleContext = new RoleContextImpl(
45                 deviceContext.getDeviceInfo(),
46                 timer, CHECK_ROLE_MASTER_TIMEOUT, config, executorService);
47
48         roleContext.setRoleService(new SalRoleServiceImpl(roleContext, deviceContext));
49         contexts.put(deviceInfo, roleContext);
50         return roleContext;
51     }
52
53     @Override
54     public void onDeviceRemoved(final DeviceInfo deviceInfo) {
55         contexts.remove(deviceInfo);
56     }
57
58     @Override
59     public void close() {
60         contexts.values().forEach(OFPContext::close);
61         contexts.clear();
62     }
63 }