c7edc72d8a82b065c32f367ca49be9ff3901f001
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / MdSalRegistratorUtils.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 java.util.concurrent.atomic.AtomicLong;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.reflect.TypeToken;
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.impl.services.FlowCapableTransactionServiceImpl;
19 import org.opendaylight.openflowplugin.impl.services.NodeConfigServiceImpl;
20 import org.opendaylight.openflowplugin.impl.services.PacketProcessingServiceImpl;
21 import org.opendaylight.openflowplugin.impl.services.SalEchoServiceImpl;
22 import org.opendaylight.openflowplugin.impl.services.SalExperimenterMessageServiceImpl;
23 import org.opendaylight.openflowplugin.impl.services.SalFlowServiceImpl;
24 import org.opendaylight.openflowplugin.impl.services.SalGroupServiceImpl;
25 import org.opendaylight.openflowplugin.impl.services.SalMeterServiceImpl;
26 import org.opendaylight.openflowplugin.impl.services.SalPortServiceImpl;
27 import org.opendaylight.openflowplugin.impl.services.SalTableServiceImpl;
28 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowStatisticsServiceImpl;
29 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightFlowTableStatisticsServiceImpl;
30 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightGroupStatisticsServiceImpl;
31 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightMeterStatisticsServiceImpl;
32 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightPortStatisticsServiceImpl;
33 import org.opendaylight.openflowplugin.impl.statistics.services.OpendaylightQueueStatisticsServiceImpl;
34 import org.opendaylight.openflowplugin.impl.statistics.services.compatibility.OpendaylightFlowStatisticsServiceDelegateImpl;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.echo.service.rev150305.SalEchoService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SalExperimenterMessageService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
52
53 public class MdSalRegistratorUtils {
54
55     private static final TypeToken<Delegator<OpendaylightFlowStatisticsService>> COMPOSITE_SERVICE_TYPE_TOKEN =
56             new TypeToken<Delegator<OpendaylightFlowStatisticsService>>() {
57                 //NOBODY
58             };
59
60     private MdSalRegistratorUtils() {
61         throw new IllegalStateException();
62     }
63
64
65     public static void registerServices(final RpcContext rpcContext, final DeviceContext deviceContext, final OfpRole newRole) {
66         rpcContext.registerRpcServiceImplementation(SalEchoService.class, new SalEchoServiceImpl(rpcContext, deviceContext));
67
68         if (OfpRole.BECOMEMASTER.equals(newRole)) {
69             rpcContext.registerRpcServiceImplementation(SalFlowService.class, new SalFlowServiceImpl(rpcContext, deviceContext));
70             //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
71             rpcContext.registerRpcServiceImplementation(FlowCapableTransactionService.class, new FlowCapableTransactionServiceImpl(rpcContext, deviceContext));
72             rpcContext.registerRpcServiceImplementation(SalMeterService.class, new SalMeterServiceImpl(rpcContext, deviceContext));
73             rpcContext.registerRpcServiceImplementation(SalGroupService.class, new SalGroupServiceImpl(rpcContext, deviceContext));
74             rpcContext.registerRpcServiceImplementation(SalTableService.class, new SalTableServiceImpl(rpcContext, deviceContext));
75             rpcContext.registerRpcServiceImplementation(SalPortService.class, new SalPortServiceImpl(rpcContext, deviceContext));
76             rpcContext.registerRpcServiceImplementation(PacketProcessingService.class, new PacketProcessingServiceImpl(rpcContext, deviceContext));
77             rpcContext.registerRpcServiceImplementation(NodeConfigService.class, new NodeConfigServiceImpl(rpcContext, deviceContext));
78             rpcContext.registerRpcServiceImplementation(OpendaylightFlowStatisticsService.class, new OpendaylightFlowStatisticsServiceImpl(rpcContext, deviceContext));
79             // TODO: experimenter symmetric and multipart message services
80             rpcContext.registerRpcServiceImplementation(SalExperimenterMessageService.class, new SalExperimenterMessageServiceImpl(rpcContext, deviceContext));
81
82         } else if (OfpRole.BECOMESLAVE.equals(newRole)) {
83             rpcContext.unregisterRpcServiceImplementation(SalFlowService.class);
84             //TODO: add constructors with rcpContext and deviceContext to meter, group, table constructors
85             rpcContext.unregisterRpcServiceImplementation(FlowCapableTransactionService.class);
86             rpcContext.unregisterRpcServiceImplementation(SalMeterService.class);
87             rpcContext.unregisterRpcServiceImplementation(SalGroupService.class);
88             rpcContext.unregisterRpcServiceImplementation(SalTableService.class);
89             rpcContext.unregisterRpcServiceImplementation(SalPortService.class);
90             rpcContext.unregisterRpcServiceImplementation(PacketProcessingService.class);
91             rpcContext.unregisterRpcServiceImplementation(NodeConfigService.class);
92             rpcContext.unregisterRpcServiceImplementation(OpendaylightFlowStatisticsService.class);
93             // TODO: experimenter symmetric and multipart message services
94             rpcContext.unregisterRpcServiceImplementation(SalExperimenterMessageService.class);
95         }
96     }
97
98     public static void unregisterServices(final RpcContext rpcContext) throws Exception {
99         rpcContext.close();
100     }
101
102     /**
103      * Support deprecated statistic related services for backward compatibility. The only exception from deprecation is
104      * the aggregated flow statistic with match criteria input.
105      *
106      * @param rpcContext
107      * @param deviceContext
108      * @param notificationPublishService
109      * @param compatibilityXidSeed
110      */
111     public static void registerStatCompatibilityServices(final RpcContext rpcContext, final DeviceContext deviceContext,
112                                                          final NotificationPublishService notificationPublishService,
113                                                          final AtomicLong compatibilityXidSeed) {
114         // pickup low statistics service
115         final OpendaylightFlowStatisticsService flowStatisticsService = Preconditions.checkNotNull(
116                 rpcContext.lookupRpcService(OpendaylightFlowStatisticsService.class));
117         Preconditions.checkArgument(COMPOSITE_SERVICE_TYPE_TOKEN.isAssignableFrom(flowStatisticsService.getClass()));
118         // attach delegate to flow statistics service (to cover all but aggregated stats with match filter input)
119         OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsDelegate =
120                 new OpendaylightFlowStatisticsServiceDelegateImpl(rpcContext, deviceContext, notificationPublishService, new AtomicLong());
121         ((Delegator<OpendaylightFlowStatisticsService>) flowStatisticsService).setDelegate(flowStatisticsDelegate);
122
123         // register all statistics (deprecated) services
124         rpcContext.registerRpcServiceImplementation(OpendaylightFlowTableStatisticsService.class,
125                 new OpendaylightFlowTableStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
126         rpcContext.registerRpcServiceImplementation(OpendaylightGroupStatisticsService.class,
127                 new OpendaylightGroupStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
128         rpcContext.registerRpcServiceImplementation(OpendaylightMeterStatisticsService.class,
129                 new OpendaylightMeterStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
130         rpcContext.registerRpcServiceImplementation(OpendaylightQueueStatisticsService.class,
131                 new OpendaylightQueueStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
132         rpcContext.registerRpcServiceImplementation(OpendaylightPortStatisticsService.class,
133                 new OpendaylightPortStatisticsServiceImpl(rpcContext, deviceContext, compatibilityXidSeed, notificationPublishService));
134     }
135 }