Add RoleManager and RoleContext
[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 javax.annotation.Nonnull;
15 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
18 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
19 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
20 import org.opendaylight.openflowplugin.impl.services.sal.SalRoleServiceImpl;
21
22 public class RoleManagerImpl implements RoleManager {
23     // Timeout after what we will give up on waiting for master role
24     private static final long CHECK_ROLE_MASTER_TIMEOUT = 20000;
25
26     private final ConcurrentMap<DeviceInfo, RoleContext> contexts = new ConcurrentHashMap<>();
27     private final HashedWheelTimer timer;
28
29     public RoleManagerImpl(final HashedWheelTimer timer) {
30         this.timer = timer;
31     }
32
33     @Override
34     public RoleContext createContext(@Nonnull final DeviceContext deviceContext) {
35         final DeviceInfo deviceInfo = deviceContext.getDeviceInfo();
36         final RoleContextImpl roleContext = new RoleContextImpl(
37                 deviceContext.getDeviceInfo(),
38                 timer, CHECK_ROLE_MASTER_TIMEOUT);
39
40         roleContext.setRoleService(new SalRoleServiceImpl(roleContext, deviceContext));
41         contexts.put(deviceInfo, roleContext);
42         return roleContext;
43     }
44
45     @Override
46     public void onDeviceRemoved(final DeviceInfo deviceInfo) {
47         contexts.remove(deviceInfo);
48     }
49
50     @Override
51     public void close() {
52         contexts.values().forEach(OFPContext::close);
53         contexts.clear();
54     }
55 }