Bump odlparent to 5.0.0
[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.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 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
39     private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
40
41     private final DeviceContext deviceContext;
42
43     public RoleService(final RequestContextStack requestContextStack,
44                        final DeviceContext deviceContext,
45                        final Class<RoleRequestOutput> clazz) {
46         super(requestContextStack, deviceContext, clazz);
47         this.deviceContext = deviceContext;
48     }
49
50     @Override
51     protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) {
52         input.setXid(xid.getValue());
53         return input.build();
54     }
55
56     public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
57         LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
58
59         // send a dummy no-change role request to get the generation-id of the switch
60         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
61         roleRequestInputBuilder.setRole(toOFJavaRole(OfpRole.NOCHANGE));
62         roleRequestInputBuilder.setVersion(version);
63         roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
64
65         final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
66         final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture =
67                 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:"
79                                 + getDeviceInfo().getNodeId().getValue()));
80                     }
81
82                 } else {
83                     LOG.error("getGenerationIdFromDevice RPC error {}",
84                             roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
85                     finalFuture.setException(new RoleChangeException(ErrorUtil
86                             .errorsToString(roleRequestOutputRpcResult.getErrors())));
87                 }
88             }
89
90             @Override
91             public void onFailure(final Throwable throwable) {
92                 LOG.info("onFailure - getGenerationIdFromDevice RPC error", throwable);
93                 finalFuture.setException(new ExecutionException(throwable));
94             }
95         }, MoreExecutors.directExecutor());
96         return finalFuture;
97     }
98
99
100     public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole,
101                                                              final Short version,
102                                                              final BigInteger generationId) {
103         LOG.info("submitRoleChange called for device:{}, role:{}",
104                 getDeviceInfo().getNodeId(), ofpRole);
105         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
106         roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
107         roleRequestInputBuilder.setVersion(version);
108         roleRequestInputBuilder.setGenerationId(generationId);
109
110         final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
111                 handleServiceCall(roleRequestInputBuilder);
112
113         final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
114         Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
115             @Override
116             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
117                 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
118                         getDeviceInfo().getNodeId(), ofpRole);
119                 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
120                 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
121                 if (roleRequestOutput != null) {
122                     final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
123                     setRoleOutputBuilder
124                             .setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
125                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
126                             .withResult(setRoleOutputBuilder.build()).build());
127
128                 } else if (rpcErrors != null) {
129                     LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
130                     for (RpcError rpcError : rpcErrors) {
131                         LOG.warn("RpcError on submitRoleChange for {}: {}",
132                                 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
133                     }
134
135                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
136                 }
137             }
138
139             @Override
140             public void onFailure(final Throwable throwable) {
141                 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
142                         getDeviceInfo().getNodeId(), ofpRole, throwable);
143                 finalFuture.setException(throwable);
144             }
145         }, MoreExecutors.directExecutor());
146         return finalFuture;
147     }
148
149     private static ControllerRole toOFJavaRole(final OfpRole role) {
150         ControllerRole ofJavaRole = null;
151         switch (role) {
152             case BECOMEMASTER:
153                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
154                 break;
155             case BECOMESLAVE:
156                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
157                 break;
158             case NOCHANGE:
159                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
160                 break;
161             default:
162                 // no intention
163                 LOG.warn("given role is not supported by protocol roles: {}", role);
164                 break;
165         }
166         return ofJavaRole;
167     }
168 }