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