Merge "Added JSON and XML payloads tabs with RFC 8040 URL"
[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 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.openflowplugin.impl.util.ErrorUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
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.opendaylight.yangtools.yang.common.Uint64;
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 ListenableFuture<Uint64> 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<Uint64> 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 ListenableFuture<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version,
101                                                                        final Uint64 generationId) {
102         LOG.info("submitRoleChange called for device:{}, role:{}",
103                 getDeviceInfo().getNodeId(), ofpRole);
104         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
105         roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
106         roleRequestInputBuilder.setVersion(version);
107         roleRequestInputBuilder.setGenerationId(generationId);
108
109         final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
110                 handleServiceCall(roleRequestInputBuilder);
111
112         final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
113         Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
114             @Override
115             public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
116                 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
117                         getDeviceInfo().getNodeId(), ofpRole);
118                 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
119                 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
120                 if (roleRequestOutput != null) {
121                     final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
122                     setRoleOutputBuilder
123                             .setTransactionId(new TransactionId(Uint64.valueOf(roleRequestOutput.getXid())));
124                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
125                             .withResult(setRoleOutputBuilder.build()).build());
126
127                 } else if (rpcErrors != null) {
128                     LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
129                     for (RpcError rpcError : rpcErrors) {
130                         LOG.warn("RpcError on submitRoleChange for {}: {}",
131                                 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
132                     }
133
134                     finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
135                 }
136             }
137
138             @Override
139             public void onFailure(final Throwable throwable) {
140                 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
141                         getDeviceInfo().getNodeId(), ofpRole, throwable);
142                 finalFuture.setException(throwable);
143             }
144         }, MoreExecutors.directExecutor());
145         return finalFuture;
146     }
147
148     private static ControllerRole toOFJavaRole(final OfpRole role) {
149         ControllerRole ofJavaRole = null;
150         switch (role) {
151             case BECOMEMASTER:
152                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
153                 break;
154             case BECOMESLAVE:
155                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
156                 break;
157             case NOCHANGE:
158                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
159                 break;
160             default:
161                 // no intention
162                 LOG.warn("given role is not supported by protocol roles: {}", role);
163                 break;
164         }
165         return ofJavaRole;
166     }
167 }