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