Bug 5692 - Direct statistics RPC
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / MdSalRegistrationUtils.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.util;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.reflect.TypeToken;
13 import java.util.concurrent.atomic.AtomicLong;
14 import javax.annotation.CheckForNull;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.compatibility.Delegator;
19 import org.opendaylight.openflowplugin.impl.services.FlowCapableTransactionServiceImpl;
20 import org.opendaylight.openflowplugin.impl.services.NodeConfigServiceImpl;
21 import org.opendaylight.openflowplugin.impl.services.PacketProcessingServiceImpl;
22 import org.opendaylight.openflowplugin.impl.services.SalEchoServiceImpl;
23 import org.opendaylight.openflowplugin.impl.services.SalExperimenterMessageServiceImpl;
24 import org.opendaylight.openflowplugin.impl.services.SalFlatBatchServiceImpl;
25 import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
26 import org.opendaylight.openflowplugin.impl.services.SalFlowsBatchServiceImpl;
27 import org.opendaylight.openflowplugin.impl.services.SalGroupServiceImpl;
28 import org.opendaylight.openflowplugin.impl.services.SalGroupsBatchServiceImpl;
29 import org.opendaylight.openflowplugin.impl.services.SalMeterServiceImpl;
30 import org.opendaylight.openflowplugin.impl.services.SalMetersBatchServiceImpl;
31 import org.opendaylight.openflowplugin.impl.services.SalPortServiceImpl;
32 import org.opendaylight.openflowplugin.impl.services.SalTableServiceImpl;
33 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowStatisticsServiceImpl;
34 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowTableStatisticsServiceImpl;
35 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightGroupStatisticsServiceImpl;
36 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightMeterStatisticsServiceImpl;
37 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightPortStatisticsServiceImpl;
38 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightQueueStatisticsServiceImpl;
39 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.OpendaylightFlowStatisticsServiceDelegateImpl;
40 import org.opendaylight.openflowplugin.impl.statistics.services.direct.FlowDirectStatisticsService;
41 import org.opendaylight.openflowplugin.impl.statistics.services.direct.GroupDirectStatisticsService;
42 import org.opendaylight.openflowplugin.impl.statistics.services.direct.MeterDirectStatisticsService;
43 import org.opendaylight.openflowplugin.impl.statistics.services.direct.NodeConnectorDirectStatisticsService;
44 import org.opendaylight.openflowplugin.impl.statistics.services.direct.OpendaylightDirectStatisticsServiceImpl;
45 import org.opendaylight.openflowplugin.impl.statistics.services.direct.OpendaylightDirectStatisticsServiceProvider;
46 import org.opendaylight.openflowplugin.impl.statistics.services.direct.QueueDirectStatisticsService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.direct.statistics.rev160511.OpendaylightDirectStatisticsService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SalExperimenterMessageService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.SalFlatBatchService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
66
67 public class MdSalRegistrationUtils {
68
69     //TODO: Make one register and one unregister method for all services
70
71     private static final TypeToken<Delegator<OpendaylightFlowStatisticsService>> COMPOSITE_SERVICE_TYPE_TOKEN =
72             new TypeToken<Delegator<OpendaylightFlowStatisticsService>>() {
73                 //NOBODY
74             };
75
76     private MdSalRegistrationUtils() {
77         throw new IllegalStateException();
78     }
79
80     /**
81      * Method registers all OF services for role {@link OfpRole#BECOMEMASTER}
82      *
83      * @param rpcContext    - registration processing is implemented in {@link RpcContext}
84      * @param deviceContext - every service needs {@link DeviceContext} as input parameter
85      * @param newRole       - role validation for {@link OfpRole#BECOMEMASTER}
86      */
87     public static void registerMasterServices(@CheckForNull final RpcContext rpcContext,
88                                               @CheckForNull final DeviceContext deviceContext, @CheckForNull final OfpRole newRole) {
89         Preconditions.checkArgument(rpcContext != null);
90         Preconditions.checkArgument(deviceContext != null);
91         Preconditions.checkArgument(newRole != null);
92         Verify.verify(OfpRole.BECOMEMASTER.equals(newRole), "Service call with bad Role {} we expect role BECOMEMASTER", newRole);
93
94         // create service instances
95         final SalFlowServiceImpl salFlowService = new SalFlowServiceImpl(rpcContext, deviceContext);
96         final FlowCapableTransactionServiceImpl flowCapableTransactionService = new FlowCapableTransactionServiceImpl(rpcContext, deviceContext);
97         final SalGroupServiceImpl salGroupService = new SalGroupServiceImpl(rpcContext, deviceContext);
98         final SalMeterServiceImpl salMeterService = new SalMeterServiceImpl(rpcContext, deviceContext);
99
100         // register routed service instances
101         rpcContext.registerRpcServiceImplementation(SalEchoService.class, new SalEchoServiceImpl(rpcContext, deviceContext));
102         rpcContext.registerRpcServiceImplementation(SalFlowService.class, salFlowService);
103         //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
104         rpcContext.registerRpcServiceImplementation(FlowCapableTransactionService.class, flowCapableTransactionService);
105         rpcContext.registerRpcServiceImplementation(SalMeterService.class, salMeterService);
106         rpcContext.registerRpcServiceImplementation(SalGroupService.class, salGroupService);
107         rpcContext.registerRpcServiceImplementation(SalTableService.class, new SalTableServiceImpl(rpcContext, deviceContext, deviceContext.getPrimaryConnectionContext().getNodeId()));
108         rpcContext.registerRpcServiceImplementation(SalPortService.class, new SalPortServiceImpl(rpcContext, deviceContext));
109         rpcContext.registerRpcServiceImplementation(PacketProcessingService.class, new PacketProcessingServiceImpl(rpcContext, deviceContext));
110         rpcContext.registerRpcServiceImplementation(NodeConfigService.class, new NodeConfigServiceImpl(rpcContext, deviceContext));
111         rpcContext.registerRpcServiceImplementation(OpendaylightFlowStatisticsService.class, OpendaylightFlowStatisticsServiceImpl.createWithOook(rpcContext, deviceContext));
112
113         // Direct statistics gathering
114         final OpendaylightDirectStatisticsServiceProvider statisticsProvider = new OpendaylightDirectStatisticsServiceProvider();
115         statisticsProvider.register(FlowDirectStatisticsService.class, new FlowDirectStatisticsService(rpcContext, deviceContext));
116         statisticsProvider.register(GroupDirectStatisticsService.class, new GroupDirectStatisticsService(rpcContext, deviceContext));
117         statisticsProvider.register(MeterDirectStatisticsService.class, new MeterDirectStatisticsService(rpcContext, deviceContext));
118         statisticsProvider.register(NodeConnectorDirectStatisticsService.class, new NodeConnectorDirectStatisticsService(rpcContext, deviceContext));
119         statisticsProvider.register(QueueDirectStatisticsService.class, new QueueDirectStatisticsService(rpcContext, deviceContext));
120         rpcContext.registerRpcServiceImplementation(OpendaylightDirectStatisticsService.class, new OpendaylightDirectStatisticsServiceImpl(statisticsProvider));
121
122         final SalFlatBatchServiceImpl salFlatBatchService = new SalFlatBatchServiceImpl(
123                 new SalFlowsBatchServiceImpl(salFlowService, flowCapableTransactionService),
124                 new SalGroupsBatchServiceImpl(salGroupService, flowCapableTransactionService),
125                 new SalMetersBatchServiceImpl(salMeterService, flowCapableTransactionService)
126         );
127         rpcContext.registerRpcServiceImplementation(SalFlatBatchService.class, salFlatBatchService);
128
129         // TODO: experimenter symmetric and multipart message services
130         rpcContext.registerRpcServiceImplementation(SalExperimenterMessageService.class, new SalExperimenterMessageServiceImpl(rpcContext, deviceContext));
131     }
132
133     /**
134      * Method unregisters all services in first step. So we don't need to call {@link MdSalRegistrationUtils#unregisterServices(RpcContext)}
135      * directly before by change role from {@link OfpRole#BECOMEMASTER} to {@link OfpRole#BECOMESLAVE}.
136      * Method registers {@link SalEchoService} in next step only because we would like to have SalEchoService as local service for all apps
137      * to be able actively check connection status for slave connection too.
138      *
139      * @param rpcContext - registration/unregistration processing is implemented in {@link RpcContext}
140      * @param newRole    - role validation for {@link OfpRole#BECOMESLAVE}
141      */
142     public static void registerSlaveServices(@CheckForNull final RpcContext rpcContext, @CheckForNull final OfpRole newRole) {
143         Preconditions.checkArgument(rpcContext != null);
144         Preconditions.checkArgument(newRole != null);
145         Verify.verify(OfpRole.BECOMESLAVE.equals(newRole), "Service call with bad Role {} we expect role BECOMESLAVE", newRole);
146
147         unregisterServices(rpcContext);
148     }
149
150     /**
151      * Method unregisters all OF services.
152      *
153      * @param rpcContext - unregistration processing is implemented in {@link RpcContext}
154      */
155     public static void unregisterServices(@CheckForNull final RpcContext rpcContext) {
156         Preconditions.checkArgument(rpcContext != null);
157
158         rpcContext.unregisterRpcServiceImplementation(SalEchoService.class);
159         rpcContext.unregisterRpcServiceImplementation(SalFlowService.class);
160         //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
161         rpcContext.unregisterRpcServiceImplementation(FlowCapableTransactionService.class);
162         rpcContext.unregisterRpcServiceImplementation(SalMeterService.class);
163         rpcContext.unregisterRpcServiceImplementation(SalGroupService.class);
164         rpcContext.unregisterRpcServiceImplementation(SalTableService.class);
165         rpcContext.unregisterRpcServiceImplementation(SalPortService.class);
166         rpcContext.unregisterRpcServiceImplementation(PacketProcessingService.class);
167         rpcContext.unregisterRpcServiceImplementation(NodeConfigService.class);
168         rpcContext.unregisterRpcServiceImplementation(OpendaylightFlowStatisticsService.class);
169         rpcContext.unregisterRpcServiceImplementation(SalFlatBatchService.class);
170         // TODO: experimenter symmetric and multipart message services
171         rpcContext.unregisterRpcServiceImplementation(SalExperimenterMessageService.class);
172         rpcContext.unregisterRpcServiceImplementation(OpendaylightDirectStatisticsService.class);
173     }
174
175     /**
176      * Support deprecated statistic related services for backward compatibility. The only exception from deprecation is
177      * the aggregated flow statistic with match criteria input.
178      *
179      * @param rpcContext
180      * @param deviceContext
181      * @param notificationPublishService
182      * @param compatibilityXidSeed
183      */
184     public static void registerStatCompatibilityServices(final RpcContext rpcContext, final DeviceContext deviceContext,
185                                                          final NotificationPublishService notificationPublishService,
186                                                          final AtomicLong compatibilityXidSeed) {
187         // pickup low statistics service
188         final OpendaylightFlowStatisticsService flowStatisticsService = Preconditions.checkNotNull(
189                 rpcContext.lookupRpcService(OpendaylightFlowStatisticsService.class));
190         Preconditions.checkArgument(COMPOSITE_SERVICE_TYPE_TOKEN.isAssignableFrom(flowStatisticsService.getClass()));
191         // attach delegate to flow statistics service (to cover all but aggregated stats with match filter input)
192         final OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsDelegate =
193                 new OpendaylightFlowStatisticsServiceDelegateImpl(rpcContext, deviceContext, notificationPublishService, new AtomicLong());
194         ((Delegator<OpendaylightFlowStatisticsService>) flowStatisticsService).setDelegate(flowStatisticsDelegate);
195
196         // register all statistics (deprecated) services
197         rpcContext.registerRpcServiceImplementation(OpendaylightFlowTableStatisticsService.class,
198                 new OpendaylightFlowTableStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
199         rpcContext.registerRpcServiceImplementation(OpendaylightGroupStatisticsService.class,
200                 new OpendaylightGroupStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
201         rpcContext.registerRpcServiceImplementation(OpendaylightMeterStatisticsService.class,
202                 new OpendaylightMeterStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
203         rpcContext.registerRpcServiceImplementation(OpendaylightQueueStatisticsService.class,
204                 new OpendaylightQueueStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
205         rpcContext.registerRpcServiceImplementation(OpendaylightPortStatisticsService.class,
206                 new OpendaylightPortStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
207     }
208 }