Bug 5596 Created lifecycle service
[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.reflect.TypeToken;
12 import java.util.concurrent.atomic.AtomicLong;
13 import javax.annotation.CheckForNull;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.compatibility.Delegator;
18 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
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.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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 import org.opendaylight.yangtools.yang.binding.RpcService;
68
69 public class MdSalRegistrationUtils {
70
71     //TODO: Make one register and one unregister method for all services
72
73     private static final TypeToken<Delegator<OpendaylightFlowStatisticsService>> COMPOSITE_SERVICE_TYPE_TOKEN =
74             new TypeToken<Delegator<OpendaylightFlowStatisticsService>>() {
75                 //NOBODY
76             };
77
78     private MdSalRegistrationUtils() {
79         throw new IllegalStateException();
80     }
81
82     /**
83      * Method registers all OF services for role {@link OfpRole#BECOMEMASTER}
84      *  @param rpcContext    - registration processing is implemented in {@link org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext}
85      * @param deviceContext - every service needs {@link org.opendaylight.openflowplugin.api.openflow.device.DeviceContext} as input parameter
86      * @param convertorExecutor
87      */
88     public static void registerServices(@CheckForNull final RpcContext rpcContext,
89                                         @CheckForNull final DeviceContext deviceContext,
90                                         final ExtensionConverterProvider extensionConverterProvider,
91                                         final ConvertorExecutor convertorExecutor) {
92         Preconditions.checkArgument(rpcContext != null);
93         Preconditions.checkArgument(deviceContext != null);
94
95         // create service instances
96         final SalFlowServiceImpl salFlowService = new SalFlowServiceImpl(rpcContext, deviceContext, convertorExecutor);
97         final FlowCapableTransactionServiceImpl flowCapableTransactionService = new FlowCapableTransactionServiceImpl(rpcContext, deviceContext);
98         final SalGroupServiceImpl salGroupService = new SalGroupServiceImpl(rpcContext, deviceContext, convertorExecutor);
99         final SalMeterServiceImpl salMeterService = new SalMeterServiceImpl(rpcContext, deviceContext, convertorExecutor);
100
101         // register routed service instances
102         rpcContext.registerRpcServiceImplementation(SalEchoService.class, new SalEchoServiceImpl(rpcContext, deviceContext));
103         rpcContext.registerRpcServiceImplementation(SalFlowService.class, salFlowService);
104         //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
105         rpcContext.registerRpcServiceImplementation(FlowCapableTransactionService.class, flowCapableTransactionService);
106         rpcContext.registerRpcServiceImplementation(SalMeterService.class, salMeterService);
107         rpcContext.registerRpcServiceImplementation(SalGroupService.class, salGroupService);
108         rpcContext.registerRpcServiceImplementation(SalTableService.class, new SalTableServiceImpl(rpcContext, deviceContext, convertorExecutor));
109         rpcContext.registerRpcServiceImplementation(SalPortService.class, new SalPortServiceImpl(rpcContext, deviceContext, convertorExecutor));
110         rpcContext.registerRpcServiceImplementation(PacketProcessingService.class, new PacketProcessingServiceImpl(rpcContext, deviceContext, convertorExecutor));
111         rpcContext.registerRpcServiceImplementation(NodeConfigService.class, new NodeConfigServiceImpl(rpcContext, deviceContext));
112         rpcContext.registerRpcServiceImplementation(OpendaylightFlowStatisticsService.class, OpendaylightFlowStatisticsServiceImpl.createWithOook(rpcContext, deviceContext, convertorExecutor));
113
114         // Direct statistics gathering
115         final OpendaylightDirectStatisticsServiceProvider statisticsProvider = new OpendaylightDirectStatisticsServiceProvider();
116         statisticsProvider.register(FlowDirectStatisticsService.class, new FlowDirectStatisticsService(rpcContext, deviceContext, convertorExecutor));
117         statisticsProvider.register(GroupDirectStatisticsService.class, new GroupDirectStatisticsService(rpcContext, deviceContext, convertorExecutor));
118         statisticsProvider.register(MeterDirectStatisticsService.class, new MeterDirectStatisticsService(rpcContext, deviceContext, convertorExecutor));
119         statisticsProvider.register(NodeConnectorDirectStatisticsService.class, new NodeConnectorDirectStatisticsService(rpcContext, deviceContext, convertorExecutor));
120         statisticsProvider.register(QueueDirectStatisticsService.class, new QueueDirectStatisticsService(rpcContext, deviceContext, convertorExecutor));
121         rpcContext.registerRpcServiceImplementation(OpendaylightDirectStatisticsService.class, new OpendaylightDirectStatisticsServiceImpl(statisticsProvider));
122
123         final SalFlatBatchServiceImpl salFlatBatchService = new SalFlatBatchServiceImpl(
124                 new SalFlowsBatchServiceImpl(salFlowService, flowCapableTransactionService),
125                 new SalGroupsBatchServiceImpl(salGroupService, flowCapableTransactionService),
126                 new SalMetersBatchServiceImpl(salMeterService, flowCapableTransactionService)
127         );
128         rpcContext.registerRpcServiceImplementation(SalFlatBatchService.class, salFlatBatchService);
129
130         // TODO: experimenter symmetric and multipart message services
131         rpcContext.registerRpcServiceImplementation(SalExperimenterMessageService.class,
132                 new SalExperimenterMessageServiceImpl(rpcContext, deviceContext, extensionConverterProvider));
133     }
134
135     /**
136      * Method unregisters all OF services.
137      *
138      * @param rpcContext - unregistration processing is implemented in {@link RpcContext}
139      */
140     public static void unregisterServices(@CheckForNull final RpcContext rpcContext) {
141         Preconditions.checkArgument(rpcContext != null);
142
143         rpcContext.unregisterRpcServiceImplementation(SalEchoService.class);
144         rpcContext.unregisterRpcServiceImplementation(SalFlowService.class);
145         //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
146         rpcContext.unregisterRpcServiceImplementation(FlowCapableTransactionService.class);
147         rpcContext.unregisterRpcServiceImplementation(SalMeterService.class);
148         rpcContext.unregisterRpcServiceImplementation(SalGroupService.class);
149         rpcContext.unregisterRpcServiceImplementation(SalTableService.class);
150         rpcContext.unregisterRpcServiceImplementation(SalPortService.class);
151         rpcContext.unregisterRpcServiceImplementation(PacketProcessingService.class);
152         rpcContext.unregisterRpcServiceImplementation(NodeConfigService.class);
153         rpcContext.unregisterRpcServiceImplementation(OpendaylightFlowStatisticsService.class);
154         rpcContext.unregisterRpcServiceImplementation(SalFlatBatchService.class);
155         // TODO: experimenter symmetric and multipart message services
156         rpcContext.unregisterRpcServiceImplementation(SalExperimenterMessageService.class);
157         rpcContext.unregisterRpcServiceImplementation(OpendaylightDirectStatisticsService.class);
158     }
159
160     /**
161      * Support deprecated statistic related services for backward compatibility. The only exception from deprecation is
162      * the aggregated flow statistic with match criteria input.
163      *  @param rpcContext
164      * @param deviceContext
165      * @param notificationPublishService
166      * @param convertorExecutor
167      */
168     public static void registerStatCompatibilityServices(final RpcContext rpcContext, final DeviceContext deviceContext,
169                                                          final NotificationPublishService notificationPublishService,
170                                                          final ConvertorExecutor convertorExecutor) {
171
172         AtomicLong compatibilityXidSeed = new AtomicLong();
173         // pickup low statistics service
174         final OpendaylightFlowStatisticsService flowStatisticsService = Preconditions.checkNotNull(
175                 rpcContext.lookupRpcService(OpendaylightFlowStatisticsService.class));
176         Preconditions.checkArgument(COMPOSITE_SERVICE_TYPE_TOKEN.isAssignableFrom(flowStatisticsService.getClass()));
177         // attach delegate to flow statistics service (to cover all but aggregated stats with match filter input)
178         final OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsDelegate =
179                 new OpendaylightFlowStatisticsServiceDelegateImpl(rpcContext, deviceContext, notificationPublishService, new AtomicLong(), convertorExecutor);
180         ((Delegator<OpendaylightFlowStatisticsService>) flowStatisticsService).setDelegate(flowStatisticsDelegate);
181
182         // register all statistics (deprecated) services
183         rpcContext.registerRpcServiceImplementation(OpendaylightFlowTableStatisticsService.class,
184                 new OpendaylightFlowTableStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
185         rpcContext.registerRpcServiceImplementation(OpendaylightGroupStatisticsService.class,
186                 new OpendaylightGroupStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService, convertorExecutor));
187         rpcContext.registerRpcServiceImplementation(OpendaylightMeterStatisticsService.class,
188                 new OpendaylightMeterStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService, convertorExecutor));
189         rpcContext.registerRpcServiceImplementation(OpendaylightQueueStatisticsService.class,
190                 new OpendaylightQueueStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
191         rpcContext.registerRpcServiceImplementation(OpendaylightPortStatisticsService.class,
192                 new OpendaylightPortStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
193     }
194 }