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