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