aaf6053930ce6a8f8196c724f33de71c4aff4ee4
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleContextImpl.java
1 /**
2  * Copyright (c) 2015 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 package org.opendaylight.openflowplugin.impl.role;
9
10 import javax.annotation.Nullable;
11 import java.util.concurrent.Future;
12
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.JdkFutureAdapters;
18 import com.google.common.util.concurrent.SettableFuture;
19 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
28 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
29 import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Created by kramesha on 9/12/15.
42  */
43 public class RoleContextImpl implements RoleContext {
44     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
45
46     private final EntityOwnershipService entityOwnershipService;
47     private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
48     private final DeviceContext deviceContext;
49     private final Entity entity;
50     private SalRoleService salRoleService;
51
52     private final SettableFuture<OfpRole> initRoleChangeFuture;
53
54     public RoleContextImpl(final DeviceContext deviceContext, final EntityOwnershipService entityOwnershipService, final Entity entity) {
55         this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
56         this.deviceContext = Preconditions.checkNotNull(deviceContext);
57         this.entity = Preconditions.checkNotNull(entity);
58
59         salRoleService = new SalRoleServiceImpl(this, deviceContext);
60
61         initRoleChangeFuture = SettableFuture.create();
62     }
63
64     @Override
65     public Future<OfpRole> initialization() throws CandidateAlreadyRegisteredException {
66         LOG.debug("Initialization requestOpenflowEntityOwnership for entity {}", entity);
67         entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity);
68         LOG.info("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext
69                 .getPrimaryConnectionContext().getNodeId().getValue());
70         return initRoleChangeFuture;
71     }
72
73     @Override
74     public void onRoleChanged(final OfpRole oldRole, final OfpRole newRole) {
75         LOG.trace("onRoleChanged method call for Entity {}", entity);
76
77         if (!isDeviceConnected()) {
78             // this can happen as after the disconnect, we still get a last messsage from EntityOwnershipService.
79             LOG.info("Device {} is disconnected from this node. Hence not attempting a role change.",
80                     deviceContext.getPrimaryConnectionContext().getNodeId());
81             if (!initRoleChangeFuture.isDone()) {
82                 LOG.debug("RoleChange is not valid for initialization Entity {} anymore - Device is disconnected", entity);
83                 initRoleChangeFuture.cancel(true);
84             }
85             return;
86         }
87
88         if (!initRoleChangeFuture.isDone()) {
89             LOG.debug("Initialization Role for entity {} is chosed {}", entity, newRole);
90             // we don't want to wait for Device RoleChangeResponse in initial phase
91             deviceContext.onClusterRoleChange(newRole);
92             initRoleChangeFuture.set(newRole);
93         }
94
95         LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole,
96                 deviceContext.getPrimaryConnectionContext().getNodeId());
97
98         final SetRoleInput setRoleInput = (new SetRoleInputBuilder())
99                 .setControllerRole(newRole)
100                 .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier()))
101                 .build();
102
103         final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture = salRoleService.setRole(setRoleInput);
104
105         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), new FutureCallback<RpcResult<SetRoleOutput>>() {
106             @Override
107             public void onSuccess(final RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
108                 LOG.debug("Rolechange {} successful made on switch :{}", newRole,
109                         deviceContext.getPrimaryConnectionContext().getNodeId());
110                 deviceContext.getDeviceState().setRole(newRole);
111                 deviceContext.onClusterRoleChange(newRole);
112             }
113
114             @Override
115             public void onFailure(final Throwable throwable) {
116                 LOG.error("Error in setRole {} for device {} ", newRole,
117                         deviceContext.getPrimaryConnectionContext().getNodeId(), throwable);
118             }
119         });
120     }
121
122     @Override
123     public void close() {
124         if (entityOwnershipCandidateRegistration != null) {
125             LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity);
126             entityOwnershipCandidateRegistration.close();
127         }
128     }
129
130     @Override
131     public Entity getEntity() {
132         return entity;
133     }
134
135     private boolean isDeviceConnected() {
136         return ConnectionContext.CONNECTION_STATE.WORKING.equals(
137                 deviceContext.getPrimaryConnectionContext().getConnectionState());
138     }
139
140     @Nullable
141     @Override
142     public <T> RequestContext<T> createRequestContext() {
143         final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
144             @Override
145             public void close() {
146             }
147         };
148         return ret;
149     }
150
151     @VisibleForTesting
152     void setSalRoleService(final SalRoleService salRoleService) {
153         this.salRoleService = salRoleService;
154     }
155
156     @Override
157     public DeviceState getDeviceState() {
158         return deviceContext.getDeviceState();
159     }
160 }