synchronization of device context
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalMeterServiceImpl.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.services;
9
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.JdkFutureAdapters;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.concurrent.Future;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.MeterConvertor;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30
31 public class SalMeterServiceImpl extends CommonService implements SalMeterService {
32
33     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalMeterServiceImpl.class);
34
35     public SalMeterServiceImpl(RequestContextStack requestContextStack, DeviceContext deviceContext) {
36         super(requestContextStack, deviceContext);
37     }
38
39     @Override
40     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
41         deviceContext.getDeviceMeterRegistry().store(input.getMeterId());
42         return this.<AddMeterOutput, Void>handleServiceCall( PRIMARY_CONNECTION,
43                  new Function<DataCrate<AddMeterOutput>,ListenableFuture<RpcResult<Void>>>() {
44                     @Override
45                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<AddMeterOutput> data) {
46                         return convertAndSend(input, data);
47                     }
48                 });
49     }
50
51     @Override
52     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
53         return this.<UpdateMeterOutput, Void>handleServiceCall( PRIMARY_CONNECTION,
54                  new Function<DataCrate<UpdateMeterOutput>,ListenableFuture<RpcResult<Void>>>() {
55                     @Override
56                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<UpdateMeterOutput> data) {
57                         return convertAndSend(input.getUpdatedMeter(), data);
58                     }
59                 });
60     }
61
62     @Override
63     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
64         deviceContext.getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
65         return this.<RemoveMeterOutput, Void>handleServiceCall( PRIMARY_CONNECTION,
66                  new Function<DataCrate<RemoveMeterOutput>,ListenableFuture<RpcResult<Void>>>() {
67                     @Override
68                     public ListenableFuture<RpcResult<Void>> apply(final DataCrate<RemoveMeterOutput> data) {
69                         return convertAndSend(input, data);
70                     }
71                 });
72     }
73
74     <T> ListenableFuture<RpcResult<Void>> convertAndSend(final Meter iputMeter, final DataCrate<T> data) {
75         messageSpy.spyMessage(iputMeter.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMITTED_SUCCESS);
76
77         final MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(iputMeter, version);
78         final Xid xid = data.getRequestContext().getXid();
79         ofMeterModInput.setXid(xid.getValue());
80         return JdkFutureAdapters.listenInPoolThread(provideConnectionAdapter(data.getiDConnection()).meterMod(ofMeterModInput.build()));
81     }
82 }