10ee6a4d0dd0ceafdc4286e98c2feec3d2b28240
[openflowplugin.git] / test-provider / src / main / java / org / opendaylight / openflowplugin / test / OpenflowpluginTestServiceProvider.java
1 /**
2  * Copyright (c) 2013 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
9 package org.opendaylight.openflowplugin.test;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
14 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
29 import org.opendaylight.yangtools.concepts.ObjectRegistration;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class OpenflowpluginTestServiceProvider implements AutoCloseable,
37         SalFlowService {
38
39     private static final Logger LOG = LoggerFactory
40             .getLogger(OpenflowpluginTestServiceProvider.class);
41
42     private final DataBroker dataService;
43     private RoutedRpcRegistration<SalFlowService> flowRegistration;
44     private final NotificationProviderService notificationProviderService;
45
46     public OpenflowpluginTestServiceProvider(DataBroker dataService,
47             NotificationProviderService notificationProviderService) {
48         this.dataService = dataService;
49         this.notificationProviderService = notificationProviderService;
50     }
51
52     /**
53      * Get data service.
54      *
55      * @return {@link #dataService}
56      */
57     public DataBroker getDataService() {
58         return dataService;
59     }
60
61     /**
62      * Get flow registration.
63      *
64      * @return {@link #flowRegistration}
65      */
66     public RoutedRpcRegistration<SalFlowService> getFlowRegistration() {
67         return flowRegistration;
68     }
69
70     /**
71      * Set {@link #flowRegistration}.
72      */
73     public void setFlowRegistration(
74             final RoutedRpcRegistration<SalFlowService> flowRegistration) {
75         this.flowRegistration = flowRegistration;
76     }
77
78     /**
79      * Get notification service.
80      *
81      * @return {@link #notificationProviderService}
82      */
83     public NotificationProviderService getNotificationService() {
84         return notificationProviderService;
85     }
86
87     /*
88      * (non-Javadoc)
89      *
90      * @see java.lang.AutoCloseable#close()
91      */
92     @Override
93     public void close() throws Exception {
94         OpenflowpluginTestServiceProvider.LOG
95                 .info("SalFlowServiceProvide stopped.");
96         flowRegistration.close();
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see
103      * org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.
104      * SalFlowService
105      * #addFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.
106      * service.rev130819.AddFlowInput)
107      */
108     @Override
109     public ListenableFuture<RpcResult<AddFlowOutput>> addFlow(AddFlowInput input) {
110         String plus = "addFlow - " + input;
111         OpenflowpluginTestServiceProvider.LOG.info(plus);
112         return null;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see
119      * org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.
120      * SalFlowService
121      * #removeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow
122      * .service.rev130819.RemoveFlowInput)
123      */
124     @Override
125     public ListenableFuture<RpcResult<RemoveFlowOutput>> removeFlow(RemoveFlowInput input) {
126         String plus = "removeFlow - " + input;
127         OpenflowpluginTestServiceProvider.LOG.info(plus);
128         return null;
129     }
130
131     /*
132      * (non-Javadoc)
133      *
134      * @see
135      * org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.
136      * SalFlowService
137      * #updateFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow
138      * .service.rev130819.UpdateFlowInput)
139      */
140     @Override
141     public ListenableFuture<RpcResult<UpdateFlowOutput>> updateFlow(UpdateFlowInput input) {
142         String plus = "updateFlow - " + input;
143         OpenflowpluginTestServiceProvider.LOG.info(plus);
144         return null;
145     }
146
147     public ObjectRegistration<OpenflowpluginTestServiceProvider> register(RpcProviderRegistry rpcRegistry) {
148         RoutedRpcRegistration<SalFlowService> addRoutedRpcImplementation =
149                 rpcRegistry.addRoutedRpcImplementation(SalFlowService.class, this);
150
151         setFlowRegistration(addRoutedRpcImplementation);
152
153         InstanceIdentifierBuilder<Nodes> builderII = InstanceIdentifier
154                 .<Nodes>builder(Nodes.class);
155
156         NodeId nodeId = new NodeId(OpenflowpluginTestActivator.NODE_ID);
157         NodeKey nodeKey = new NodeKey(nodeId);
158
159         InstanceIdentifierBuilder<Node> nodeIdentifier = builderII
160                 .<Node, NodeKey>child(Node.class, nodeKey);
161
162         InstanceIdentifier<Node> instance = nodeIdentifier.build();
163
164         flowRegistration.registerPath(NodeContext.class, instance);
165
166         RoutedRpcRegistration<SalFlowService> flowRegistration2 = getFlowRegistration();
167
168         return new AbstractObjectRegistration<OpenflowpluginTestServiceProvider>(this) {
169             @Override
170             protected void removeRegistration() {
171                 flowRegistration2.close();
172             }
173         };
174     }
175 }