Fix connection when slave role request is unsupported
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / RoleService.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.services;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.math.BigInteger;
15 import java.util.Collection;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
22 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
23 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Created by kramesha on 8/24/15.
40  */
41 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
42     private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
43
44     private final DeviceContext deviceContext;
45
46     public RoleService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final Class<RoleRequestOutput> clazz) {
47         super(requestContextStack, deviceContext, clazz);
48         this.deviceContext = deviceContext;
49     }
50
51     @Override
52     protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) throws ServiceException {
53         input.setXid(xid.getValue());
54         return input.build();
55     }
56
57     public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
58         LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
59
60         // send a dummy no-change role request to get the generation-id of the switch
61         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
62         roleRequestInputBuilder.setRole(toOFJavaRole(OfpRole.NOCHANGE));
63         roleRequestInputBuilder.setVersion(version);
64         roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
65
66         final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
67         final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture = handleServiceCall(roleRequestInputBuilder);
68         Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
69             @Override
70             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
71                 if (roleRequestOutputRpcResult.isSuccessful()) {
72                     final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
73                     if (roleRequestOutput != null) {
74                         LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
75                         finalFuture.set(roleRequestOutput.getGenerationId());
76                     } else {
77                         LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
78                         finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:" + getDeviceInfo().getNodeId().getValue()));
79                     }
80
81                 } else {
82                     LOG.error("getGenerationIdFromDevice RPC error " +
83                             roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
84                     finalFuture.setException(new RoleChangeException(ErrorUtil.errorsToString(roleRequestOutputRpcResult.getErrors())));
85                 }
86             }
87
88             @Override
89             public void onFailure(final Throwable throwable) {
90                 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
91                 finalFuture.setException(new ExecutionException(throwable));
92             }
93         });
94         return finalFuture;
95     }
96
97
98     public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version, final BigInteger generationId) {
99         LOG.info("submitRoleChange called for device:{}, role:{}",
100                 getDeviceInfo().getNodeId(), ofpRole);
101         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
102         roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
103         roleRequestInputBuilder.setVersion(version);
104         roleRequestInputBuilder.setGenerationId(generationId);
105
106         final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
107
108         final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
109         Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
110             @Override
111             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
112                 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
113                         getDeviceInfo().getNodeId(), ofpRole);
114                 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
115                 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
116                 if (roleRequestOutput != null) {
117                     final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
118                     setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
119                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>success().withResult(setRoleOutputBuilder.build()).build());
120
121                 } else if (rpcErrors != null) {
122                     LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
123                     for (RpcError rpcError : rpcErrors) {
124                         LOG.warn("RpcError on submitRoleChange for {}: {}",
125                                 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
126                     }
127
128                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
129                 }
130             }
131
132             @Override
133             public void onFailure(final Throwable throwable) {
134                 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
135                         getDeviceInfo().getNodeId(), ofpRole, throwable);
136                 finalFuture.setException(throwable);
137             }
138         });
139         return finalFuture;
140     }
141
142     private static ControllerRole toOFJavaRole(final OfpRole role) {
143         ControllerRole ofJavaRole = null;
144         switch (role) {
145             case BECOMEMASTER:
146                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
147                 break;
148             case BECOMESLAVE:
149                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
150                 break;
151             case NOCHANGE:
152                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
153                 break;
154             default:
155                 // no intention
156                 LOG.warn("given role is not supported by protocol roles: {}", role);
157                 break;
158         }
159         return ofJavaRole;
160     }
161
162
163 }