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