Post "Clustering optimization" updates
[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.SalFlowServiceImpl;
25 import org.opendaylight.openflowplugin.impl.services.SalGroupServiceImpl;
26 import org.opendaylight.openflowplugin.impl.services.SalMeterServiceImpl;
27 import org.opendaylight.openflowplugin.impl.services.SalPortServiceImpl;
28 import org.opendaylight.openflowplugin.impl.services.SalTableServiceImpl;
29 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowStatisticsServiceImpl;
30 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowTableStatisticsServiceImpl;
31 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightGroupStatisticsServiceImpl;
32 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightMeterStatisticsServiceImpl;
33 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightPortStatisticsServiceImpl;
34 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightQueueStatisticsServiceImpl;
35 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.OpendaylightFlowStatisticsServiceDelegateImpl;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SalExperimenterMessageService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
53
54 public class MdSalRegistrationUtils {
55
56     //TODO: Make one register and one unregister method for all services
57
58     private static final TypeToken<Delegator<OpendaylightFlowStatisticsService>> COMPOSITE_SERVICE_TYPE_TOKEN =
59             new TypeToken<Delegator<OpendaylightFlowStatisticsService>>() {
60                 //NOBODY
61             };
62
63     private MdSalRegistrationUtils() {
64         throw new IllegalStateException();
65     }
66
67     /**
68      * Method registers all OF services for role {@link OfpRole#BECOMEMASTER}
69      * @param rpcContext - registration processing is implemented in {@link RpcContext}
70      * @param deviceContext - every service needs {@link DeviceContext} as input parameter
71      * @param newRole - role validation for {@link OfpRole#BECOMEMASTER}
72      */
73     public static void registerMasterServices(@CheckForNull final RpcContext rpcContext,
74             @CheckForNull final DeviceContext deviceContext, @CheckForNull final OfpRole newRole) {
75         Preconditions.checkArgument(rpcContext != null);
76         Preconditions.checkArgument(deviceContext != null);
77         Preconditions.checkArgument(newRole != null);
78         Verify.verify(OfpRole.BECOMEMASTER.equals(newRole), "Service call with bad Role {} we expect role BECOMEMASTER", newRole);
79
80         rpcContext.registerRpcServiceImplementation(SalEchoService.class, new SalEchoServiceImpl(rpcContext, deviceContext));
81
82         rpcContext.registerRpcServiceImplementation(SalFlowService.class, new SalFlowServiceImpl(rpcContext, deviceContext));
83         //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
84         rpcContext.registerRpcServiceImplementation(FlowCapableTransactionService.class, new FlowCapableTransactionServiceImpl(rpcContext, deviceContext));
85         rpcContext.registerRpcServiceImplementation(SalMeterService.class, new SalMeterServiceImpl(rpcContext, deviceContext));
86         rpcContext.registerRpcServiceImplementation(SalGroupService.class, new SalGroupServiceImpl(rpcContext, deviceContext));
87         rpcContext.registerRpcServiceImplementation(SalTableService.class, new SalTableServiceImpl(rpcContext, deviceContext));
88         rpcContext.registerRpcServiceImplementation(SalPortService.class, new SalPortServiceImpl(rpcContext, deviceContext));
89         rpcContext.registerRpcServiceImplementation(PacketProcessingService.class, new PacketProcessingServiceImpl(rpcContext, deviceContext));
90         rpcContext.registerRpcServiceImplementation(NodeConfigService.class, new NodeConfigServiceImpl(rpcContext, deviceContext));
91         rpcContext.registerRpcServiceImplementation(OpendaylightFlowStatisticsService.class, new OpendaylightFlowStatisticsServiceImpl(rpcContext, deviceContext));
92         // TODO: experimenter symmetric and multipart message services
93         rpcContext.registerRpcServiceImplementation(SalExperimenterMessageService.class, new SalExperimenterMessageServiceImpl(rpcContext, deviceContext));
94     }
95
96     /**
97      * Method unregisters all services in first step. So we don't need to call {@link MdSalRegistrationUtils#unregisterServices(RpcContext)}
98      * directly before by change role from {@link OfpRole#BECOMEMASTER} to {@link OfpRole#BECOMESLAVE}.
99      * Method registers {@link SalEchoService} in next step only because we would like to have SalEchoService as local service for all apps
100      * to be able actively check connection status for slave connection too.
101      * @param rpcContext - registration/unregistration processing is implemented in {@link RpcContext}
102      * @param newRole - role validation for {@link OfpRole#BECOMESLAVE}
103      */
104     public static void registerSlaveServices(@CheckForNull final RpcContext rpcContext, @CheckForNull final OfpRole newRole) {
105         Preconditions.checkArgument(rpcContext != null);
106         Preconditions.checkArgument(newRole != null);
107         Verify.verify(OfpRole.BECOMESLAVE.equals(newRole), "Service call with bad Role {} we expect role BECOMESLAVE", newRole);
108
109         unregisterServices(rpcContext);
110     }
111
112     /**
113      * Method unregisters all OF services.
114      * @param rpcContext - unregistration processing is implemented in {@link RpcContext}
115      */
116     public static void unregisterServices(@CheckForNull final RpcContext rpcContext) {
117         Preconditions.checkArgument(rpcContext != null);
118
119         rpcContext.unregisterRpcServiceImplementation(SalEchoService.class);
120         rpcContext.unregisterRpcServiceImplementation(SalFlowService.class);
121         //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
122         rpcContext.unregisterRpcServiceImplementation(FlowCapableTransactionService.class);
123         rpcContext.unregisterRpcServiceImplementation(SalMeterService.class);
124         rpcContext.unregisterRpcServiceImplementation(SalGroupService.class);
125         rpcContext.unregisterRpcServiceImplementation(SalTableService.class);
126         rpcContext.unregisterRpcServiceImplementation(SalPortService.class);
127         rpcContext.unregisterRpcServiceImplementation(PacketProcessingService.class);
128         rpcContext.unregisterRpcServiceImplementation(NodeConfigService.class);
129         rpcContext.unregisterRpcServiceImplementation(OpendaylightFlowStatisticsService.class);
130         // TODO: experimenter symmetric and multipart message services
131         rpcContext.unregisterRpcServiceImplementation(SalExperimenterMessageService.class);
132     }
133
134     /**
135      * Support deprecated statistic related services for backward compatibility. The only exception from deprecation is
136      * the aggregated flow statistic with match criteria input.
137      *
138      * @param rpcContext
139      * @param deviceContext
140      * @param notificationPublishService
141      * @param compatibilityXidSeed
142      */
143     public static void registerStatCompatibilityServices(final RpcContext rpcContext, final DeviceContext deviceContext,
144                                                          final NotificationPublishService notificationPublishService,
145                                                          final AtomicLong compatibilityXidSeed) {
146         // pickup low statistics service
147         final OpendaylightFlowStatisticsService flowStatisticsService = Preconditions.checkNotNull(
148                 rpcContext.lookupRpcService(OpendaylightFlowStatisticsService.class));
149         Preconditions.checkArgument(COMPOSITE_SERVICE_TYPE_TOKEN.isAssignableFrom(flowStatisticsService.getClass()));
150         // attach delegate to flow statistics service (to cover all but aggregated stats with match filter input)
151         final OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsDelegate =
152                 new OpendaylightFlowStatisticsServiceDelegateImpl(rpcContext, deviceContext, notificationPublishService, new AtomicLong());
153         ((Delegator<OpendaylightFlowStatisticsService>) flowStatisticsService).setDelegate(flowStatisticsDelegate);
154
155         // register all statistics (deprecated) services
156         rpcContext.registerRpcServiceImplementation(OpendaylightFlowTableStatisticsService.class,
157                 new OpendaylightFlowTableStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
158         rpcContext.registerRpcServiceImplementation(OpendaylightGroupStatisticsService.class,
159                 new OpendaylightGroupStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
160         rpcContext.registerRpcServiceImplementation(OpendaylightMeterStatisticsService.class,
161                 new OpendaylightMeterStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
162         rpcContext.registerRpcServiceImplementation(OpendaylightQueueStatisticsService.class,
163                 new OpendaylightQueueStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
164         rpcContext.registerRpcServiceImplementation(OpendaylightPortStatisticsService.class,
165                 new OpendaylightPortStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
166     }
167 }