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