Merge "OPNFLWPLUG-929 : Remove deprecated guava library in openflowplugin-impl"
[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 java.util.concurrent.Future;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
23 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
24 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
40     private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
41
42     private final DeviceContext deviceContext;
43
44     public RoleService(final RequestContextStack requestContextStack,
45                        final DeviceContext deviceContext,
46                        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 =
68                 handleServiceCall(roleRequestInputBuilder);
69         Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
70             @Override
71             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
72                 if (roleRequestOutputRpcResult.isSuccessful()) {
73                     final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
74                     if (roleRequestOutput != null) {
75                         LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
76                         finalFuture.set(roleRequestOutput.getGenerationId());
77                     } else {
78                         LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
79                         finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:"
80                                 + getDeviceInfo().getNodeId().getValue()));
81                     }
82
83                 } else {
84                     LOG.error("getGenerationIdFromDevice RPC error "
85                             + roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
86                     finalFuture.setException(new RoleChangeException(ErrorUtil
87                             .errorsToString(roleRequestOutputRpcResult.getErrors())));
88                 }
89             }
90
91             @Override
92             public void onFailure(final Throwable throwable) {
93                 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
94                 finalFuture.setException(new ExecutionException(throwable));
95             }
96         }, MoreExecutors.directExecutor());
97         return finalFuture;
98     }
99
100
101     public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole,
102                                                              final Short version,
103                                                              final BigInteger generationId) {
104         LOG.info("submitRoleChange called for device:{}, role:{}",
105                 getDeviceInfo().getNodeId(), ofpRole);
106         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
107         roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
108         roleRequestInputBuilder.setVersion(version);
109         roleRequestInputBuilder.setGenerationId(generationId);
110
111         final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
112                 handleServiceCall(roleRequestInputBuilder);
113
114         final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
115         Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
116             @Override
117             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
118                 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
119                         getDeviceInfo().getNodeId(), ofpRole);
120                 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
121                 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
122                 if (roleRequestOutput != null) {
123                     final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
124                     setRoleOutputBuilder
125                             .setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
126                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
127                             .withResult(setRoleOutputBuilder.build()).build());
128
129                 } else if (rpcErrors != null) {
130                     LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
131                     for (RpcError rpcError : rpcErrors) {
132                         LOG.warn("RpcError on submitRoleChange for {}: {}",
133                                 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
134                     }
135
136                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
137                 }
138             }
139
140             @Override
141             public void onFailure(final Throwable throwable) {
142                 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
143                         getDeviceInfo().getNodeId(), ofpRole, throwable);
144                 finalFuture.setException(throwable);
145             }
146         }, MoreExecutors.directExecutor());
147         return finalFuture;
148     }
149
150     private static ControllerRole toOFJavaRole(final OfpRole role) {
151         ControllerRole ofJavaRole = null;
152         switch (role) {
153             case BECOMEMASTER:
154                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
155                 break;
156             case BECOMESLAVE:
157                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
158                 break;
159             case NOCHANGE:
160                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
161                 break;
162             default:
163                 // no intention
164                 LOG.warn("given role is not supported by protocol roles: {}", role);
165                 break;
166         }
167         return ofJavaRole;
168     }
169 }