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