Rework of handleServiceCall method
[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.FutureCallback;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import java.util.concurrent.Future;
15 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.MeterConvertor;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37
38 public class SalMeterServiceImpl extends CommonService implements SalMeterService {
39
40     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SalMeterServiceImpl.class);
41
42     public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
43         super(requestContextStack, deviceContext);
44     }
45
46     @Override
47     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
48         getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
49         return handleServiceCall(new Function<RequestContext<AddMeterOutput>, ListenableFuture<RpcResult<AddMeterOutput>>>() {
50             @Override
51             public ListenableFuture<RpcResult<AddMeterOutput>> apply(final RequestContext<AddMeterOutput> requestContext) {
52                 return convertAndSend(input, requestContext);
53             }
54         });
55     }
56
57     @Override
58     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
59         return handleServiceCall(new Function<RequestContext<UpdateMeterOutput>, ListenableFuture<RpcResult<UpdateMeterOutput>>>() {
60             @Override
61             public ListenableFuture<RpcResult<UpdateMeterOutput>> apply(final RequestContext<UpdateMeterOutput> requestContext) {
62                 return convertAndSend(input.getUpdatedMeter(), requestContext);
63             }
64         });
65     }
66
67     @Override
68     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
69         getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
70         return handleServiceCall(new Function<RequestContext<RemoveMeterOutput>, ListenableFuture<RpcResult<RemoveMeterOutput>>>() {
71             @Override
72             public ListenableFuture<RpcResult<RemoveMeterOutput>> apply(final RequestContext<RemoveMeterOutput> requestContext) {
73                 return convertAndSend(input, requestContext);
74             }
75         });
76     }
77
78     <T> ListenableFuture<RpcResult<T>> convertAndSend(final Meter iputMeter, final RequestContext<T> requestContext) {
79         getMessageSpy().spyMessage(iputMeter.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
80         final OutboundQueue outboundQueue = getDeviceContext().getPrimaryConnectionContext().getOutboundQueueProvider();
81
82         final MeterModInputBuilder ofMeterModInput = MeterConvertor.toMeterModInput(iputMeter, getVersion());
83         final Xid xid = requestContext.getXid();
84         ofMeterModInput.setXid(xid.getValue());
85         final SettableFuture<RpcResult<T>> settableFuture = SettableFuture.create();
86         final MeterModInput meterModInput = ofMeterModInput.build();
87         outboundQueue.commitEntry(xid.getValue(), meterModInput, new FutureCallback<OfHeader>() {
88             @Override
89             public void onSuccess(final OfHeader ofHeader) {
90                 RequestContextUtil.closeRequstContext(requestContext);
91                 getMessageSpy().spyMessage(meterModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_SUCCESS);
92
93                 settableFuture.set(RpcResultBuilder.<T>success().build());
94             }
95
96             @Override
97             public void onFailure(final Throwable throwable) {
98                 RpcResultBuilder<T> rpcResultBuilder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
99                 RequestContextUtil.closeRequstContext(requestContext);
100                 getMessageSpy().spyMessage(meterModInput.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
101                 settableFuture.set(rpcResultBuilder.build());
102             }
103         });
104         return settableFuture;
105     }
106 }