Merge "Bump odlparent 2.0.2 to 2.0.4"
[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 java.util.concurrent.atomic.AtomicLong;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.compatibility.Delegator;
17 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
18 import org.opendaylight.openflowplugin.extension.onf.service.SalBundleServiceImpl;
19 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
20 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
21 import org.opendaylight.openflowplugin.impl.services.sal.FlowCapableTransactionServiceImpl;
22 import org.opendaylight.openflowplugin.impl.services.sal.NodeConfigServiceImpl;
23 import org.opendaylight.openflowplugin.impl.services.sal.PacketProcessingServiceImpl;
24 import org.opendaylight.openflowplugin.impl.services.sal.SalEchoServiceImpl;
25 import org.opendaylight.openflowplugin.impl.services.sal.SalExperimenterMessageServiceImpl;
26 import org.opendaylight.openflowplugin.impl.services.sal.SalExperimenterMpMessageServiceImpl;
27 import org.opendaylight.openflowplugin.impl.services.sal.SalFlatBatchServiceImpl;
28 import org.opendaylight.openflowplugin.impl.services.sal.SalFlowServiceImpl;
29 import org.opendaylight.openflowplugin.impl.services.sal.SalFlowsBatchServiceImpl;
30 import org.opendaylight.openflowplugin.impl.services.sal.SalGroupServiceImpl;
31 import org.opendaylight.openflowplugin.impl.services.sal.SalGroupsBatchServiceImpl;
32 import org.opendaylight.openflowplugin.impl.services.sal.SalMeterServiceImpl;
33 import org.opendaylight.openflowplugin.impl.services.sal.SalMetersBatchServiceImpl;
34 import org.opendaylight.openflowplugin.impl.services.sal.SalPortServiceImpl;
35 import org.opendaylight.openflowplugin.impl.services.sal.SalTableServiceImpl;
36 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowStatisticsServiceImpl;
37 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowTableStatisticsServiceImpl;
38 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightGroupStatisticsServiceImpl;
39 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightMeterStatisticsServiceImpl;
40 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightPortStatisticsServiceImpl;
41 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightQueueStatisticsServiceImpl;
42 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.OpendaylightFlowStatisticsServiceDelegateImpl;
43 import org.opendaylight.openflowplugin.impl.statistics.services.direct.OpendaylightDirectStatisticsServiceImpl;
44 import org.opendaylight.openflowplugin.impl.statistics.services.direct.multilayer.MultiLayerDirectStatisticsProviderInitializer;
45 import org.opendaylight.openflowplugin.impl.statistics.services.direct.singlelayer.SingleLayerDirectStatisticsProviderInitializer;
46 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
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.experimenter.mp.message.service.rev151020.SalExperimenterMpMessageService;
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.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
68
69 public class MdSalRegistrationUtils {
70
71     //TODO: Make one register and one unregister method for all services
72     private MdSalRegistrationUtils() {
73         throw new IllegalStateException();
74     }
75
76     /**
77      * Method registers all OF services for role {@link OfpRole#BECOMEMASTER}
78      *  @param rpcContext    - registration processing is implemented in {@link org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext}
79      * @param deviceContext - every service needs {@link org.opendaylight.openflowplugin.api.openflow.device.DeviceContext} as input parameter
80      * @param convertorExecutor convertor executor
81      */
82     public static void registerServices(@Nonnull final RpcContext rpcContext,
83                                         @Nonnull final DeviceContext deviceContext,
84                                         final ExtensionConverterProvider extensionConverterProvider,
85                                         final ConvertorExecutor convertorExecutor) {
86         // TODO: Use multipart writer provider from device context
87         final MultipartWriterProvider multipartWriterProvider = MultipartWriterProviderFactory
88             .createDefaultProvider(deviceContext);
89
90         // create service instances
91         final SalFlowServiceImpl salFlowService = new SalFlowServiceImpl(rpcContext, deviceContext, convertorExecutor);
92         final FlowCapableTransactionServiceImpl flowCapableTransactionService = new FlowCapableTransactionServiceImpl(rpcContext, deviceContext);
93         final SalGroupServiceImpl salGroupService = new SalGroupServiceImpl(rpcContext, deviceContext, convertorExecutor);
94         final SalMeterServiceImpl salMeterService = new SalMeterServiceImpl(rpcContext, deviceContext, convertorExecutor);
95
96         // register routed service instances
97         rpcContext.registerRpcServiceImplementation(SalEchoService.class, new SalEchoServiceImpl(rpcContext, deviceContext));
98         rpcContext.registerRpcServiceImplementation(SalFlowService.class, salFlowService);
99         rpcContext.registerRpcServiceImplementation(FlowCapableTransactionService.class, flowCapableTransactionService);
100         rpcContext.registerRpcServiceImplementation(SalMeterService.class, salMeterService);
101         rpcContext.registerRpcServiceImplementation(SalGroupService.class, salGroupService);
102         rpcContext.registerRpcServiceImplementation(SalTableService.class, new SalTableServiceImpl(rpcContext, deviceContext, convertorExecutor, multipartWriterProvider));
103         rpcContext.registerRpcServiceImplementation(SalPortService.class, new SalPortServiceImpl(rpcContext, deviceContext, convertorExecutor));
104         rpcContext.registerRpcServiceImplementation(PacketProcessingService.class, new PacketProcessingServiceImpl(rpcContext, deviceContext, convertorExecutor));
105         rpcContext.registerRpcServiceImplementation(NodeConfigService.class, new NodeConfigServiceImpl(rpcContext, deviceContext));
106         rpcContext.registerRpcServiceImplementation(OpendaylightFlowStatisticsService.class, OpendaylightFlowStatisticsServiceImpl.createWithOook(rpcContext, deviceContext, convertorExecutor));
107
108         // register direct statistics gathering services
109         rpcContext.registerRpcServiceImplementation(OpendaylightDirectStatisticsService.class,
110             new OpendaylightDirectStatisticsServiceImpl(deviceContext.canUseSingleLayerSerialization()
111                 ? SingleLayerDirectStatisticsProviderInitializer
112                     .createProvider(rpcContext, deviceContext, convertorExecutor, multipartWriterProvider)
113                 : MultiLayerDirectStatisticsProviderInitializer
114                     .createProvider(rpcContext, deviceContext, convertorExecutor, multipartWriterProvider)));
115
116         // register flat batch services
117         rpcContext.registerRpcServiceImplementation(SalFlatBatchService.class, new SalFlatBatchServiceImpl(
118                 new SalFlowsBatchServiceImpl(salFlowService, flowCapableTransactionService),
119                 new SalGroupsBatchServiceImpl(salGroupService, flowCapableTransactionService),
120                 new SalMetersBatchServiceImpl(salMeterService, flowCapableTransactionService)
121         ));
122
123         // register experimenter services
124         rpcContext.registerRpcServiceImplementation(SalExperimenterMessageService.class,
125                 new SalExperimenterMessageServiceImpl(rpcContext, deviceContext, extensionConverterProvider));
126         rpcContext.registerRpcServiceImplementation(SalExperimenterMpMessageService.class,
127                 new SalExperimenterMpMessageServiceImpl(rpcContext, deviceContext, extensionConverterProvider));
128
129         //register onf extension bundles
130         rpcContext.registerRpcServiceImplementation(SalBundleService.class,
131                 new SalBundleServiceImpl(new SalExperimenterMessageServiceImpl(
132                         rpcContext, deviceContext, extensionConverterProvider
133                 )));
134     }
135
136     /**
137      * Support deprecated statistic related services for backward compatibility. The only exception from deprecation is
138      * the aggregated flow statistic with match criteria input.
139      * @param rpcContext
140      * @param deviceContext
141      * @param notificationPublishService
142      * @param convertorExecutor
143      */
144     public static void registerStatCompatibilityServices(final RpcContext rpcContext, final DeviceContext deviceContext,
145                                                          final NotificationPublishService notificationPublishService,
146                                                          final ConvertorExecutor convertorExecutor) {
147
148         AtomicLong compatibilityXidSeed = new AtomicLong();
149         // pickup low statistics service
150         final OpendaylightFlowStatisticsService flowStatisticsService = Preconditions.checkNotNull(
151                 rpcContext.lookupRpcService(OpendaylightFlowStatisticsService.class));
152
153         // attach delegate to flow statistics service (to cover all but aggregated stats with match filter input)
154         final OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsDelegate =
155                 new OpendaylightFlowStatisticsServiceDelegateImpl(rpcContext, deviceContext, notificationPublishService, new AtomicLong(), convertorExecutor);
156         ((Delegator<OpendaylightFlowStatisticsService>) flowStatisticsService).setDelegate(flowStatisticsDelegate);
157
158         // register all statistics (deprecated) services
159         rpcContext.registerRpcServiceImplementation(OpendaylightFlowTableStatisticsService.class,
160                 new OpendaylightFlowTableStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
161         rpcContext.registerRpcServiceImplementation(OpendaylightGroupStatisticsService.class,
162                 new OpendaylightGroupStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService, convertorExecutor));
163         rpcContext.registerRpcServiceImplementation(OpendaylightMeterStatisticsService.class,
164                 new OpendaylightMeterStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService, convertorExecutor));
165         rpcContext.registerRpcServiceImplementation(OpendaylightQueueStatisticsService.class,
166                 new OpendaylightQueueStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
167         rpcContext.registerRpcServiceImplementation(OpendaylightPortStatisticsService.class,
168                 new OpendaylightPortStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
169     }
170 }