0bf66fa960a12eaaf04c6279edf3097d677412cb
[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.inventory.rev130819.NodeId;
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 /**
38  * Created by kramesha on 8/24/15.
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     protected RoleService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, 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         final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
58         LOG.info("getGenerationIdFromDevice called for device:{}", nodeId.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 = 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:" + nodeId.getValue()));
79                     }
80
81                 } else {
82                     LOG.error("getGenerationIdFromDevice RPC error " +
83                             roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
84
85                 }
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         });
95         return finalFuture;
96     }
97
98
99     public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version, final BigInteger generationId) {
100         LOG.info("submitRoleChange called for device:{}, role:{}",
101                 deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole);
102         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
103         roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
104         roleRequestInputBuilder.setVersion(version);
105         roleRequestInputBuilder.setGenerationId(generationId);
106
107         final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
108
109         final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
110         Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
111             @Override
112             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
113                 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
114                         deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole);
115                 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
116                 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
117                 if (roleRequestOutput != null) {
118                     final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
119                     setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
120                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>success().withResult(setRoleOutputBuilder.build()).build());
121
122                 } else if (rpcErrors != null) {
123                     LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
124                     for (RpcError rpcError : rpcErrors) {
125                         LOG.warn("RpcError on submitRoleChange for {}: {}",
126                                 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
127                     }
128
129                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
130                 }
131             }
132
133             @Override
134             public void onFailure(final Throwable throwable) {
135                 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
136                         deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole, throwable);
137                 finalFuture.setException(throwable);
138             }
139         });
140         return finalFuture;
141     }
142
143     private static ControllerRole toOFJavaRole(final OfpRole role) {
144         ControllerRole ofJavaRole = null;
145         switch (role) {
146             case BECOMEMASTER:
147                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
148                 break;
149             case BECOMESLAVE:
150                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
151                 break;
152             case NOCHANGE:
153                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
154                 break;
155             default:
156                 // no intention
157                 LOG.warn("given role is not supported by protocol roles: {}", role);
158                 break;
159         }
160         return ofJavaRole;
161     }
162
163
164 }