Bump odlparent to 5.0.0
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / 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.sal;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
16 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerMeterService;
17 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerMeterService;
18 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class SalMeterServiceImpl implements SalMeterService {
33     private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class);
34     private final MultiLayerMeterService<AddMeterInput, AddMeterOutput> addMeter;
35     private final MultiLayerMeterService<Meter, UpdateMeterOutput> updateMeter;
36     private final MultiLayerMeterService<RemoveMeterInput, RemoveMeterOutput> removeMeter;
37     private final SingleLayerMeterService<AddMeterOutput> addMeterMessage;
38     private final SingleLayerMeterService<UpdateMeterOutput> updateMeterMessage;
39     private final SingleLayerMeterService<RemoveMeterOutput> removeMeterMessage;
40
41     private final DeviceContext deviceContext;
42
43     public SalMeterServiceImpl(final RequestContextStack requestContextStack,
44                                final DeviceContext deviceContext,
45                                final ConvertorExecutor convertorExecutor) {
46         this.deviceContext = deviceContext;
47         addMeter = new MultiLayerMeterService<>(requestContextStack,
48                 deviceContext,
49                 AddMeterOutput.class,
50                 convertorExecutor);
51
52         updateMeter = new MultiLayerMeterService<>(requestContextStack,
53                 deviceContext,
54                 UpdateMeterOutput.class,
55                 convertorExecutor);
56
57         removeMeter = new MultiLayerMeterService<>(requestContextStack,
58                 deviceContext,
59                 RemoveMeterOutput.class,
60                 convertorExecutor);
61
62         addMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
63         updateMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
64         removeMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
65     }
66
67     @Override
68     public ListenableFuture<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
69         final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture =
70                 addMeterMessage.canUseSingleLayerSerialization()
71                         ? addMeterMessage.handleServiceCall(input)
72                         : addMeter.handleServiceCall(input);
73
74         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
75             @Override
76             public void onSuccess(RpcResult<AddMeterOutput> result) {
77                 if (result.isSuccessful()) {
78                     if (LOG.isDebugEnabled()) {
79                         LOG.debug("Meter add with id={} finished without error", input.getMeterId());
80                     }
81                 } else {
82                     if (LOG.isDebugEnabled()) {
83                         LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
84                                 ErrorUtil.errorsToString(result.getErrors()));
85                     }
86                 }
87             }
88
89             @Override
90             public void onFailure(Throwable throwable) {
91                 LOG.warn("Service call for adding meter={} failed", input.getMeterId(), throwable);
92             }
93         }, MoreExecutors.directExecutor());
94         return resultFuture;
95     }
96
97     @Override
98     public ListenableFuture<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
99         final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture =
100                 updateMeterMessage.canUseSingleLayerSerialization()
101                         ? updateMeterMessage.handleServiceCall(input.getUpdatedMeter())
102                         : updateMeter.handleServiceCall(input.getUpdatedMeter());
103
104         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
105             @Override
106             public void onSuccess(RpcResult<UpdateMeterOutput> result) {
107                 if (result.isSuccessful()) {
108                     if (LOG.isDebugEnabled()) {
109                         LOG.debug("Meter update with id={} finished without error",
110                                 input.getOriginalMeter().getMeterId());
111                     }
112                 } else {
113                     LOG.warn("Meter update with id={} failed, errors={}", input.getOriginalMeter().getMeterId(),
114                             ErrorUtil.errorsToString(result.getErrors()));
115                     LOG.debug("Meter input={}", input.getUpdatedMeter());
116                 }
117             }
118
119             @Override
120             public void onFailure(Throwable throwable) {
121                 LOG.warn("Service call for updating meter={} failed",
122                         input.getOriginalMeter().getMeterId(), throwable);
123             }
124         }, MoreExecutors.directExecutor());
125         return resultFuture;
126     }
127
128     @Override
129     public ListenableFuture<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
130         final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture =
131                 removeMeterMessage.canUseSingleLayerSerialization()
132                         ? removeMeterMessage.handleServiceCall(input)
133                         : removeMeter.handleServiceCall(input);
134
135         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
136             @Override
137             public void onSuccess(RpcResult<RemoveMeterOutput> result) {
138                 if (result.isSuccessful()) {
139                     if (LOG.isDebugEnabled()) {
140                         LOG.debug("Meter remove with id={} finished without error", input.getMeterId());
141                     }
142                 } else {
143                     LOG.warn("Meter remove with id={} failed, errors={}", input.getMeterId(),
144                             ErrorUtil.errorsToString(result.getErrors()));
145                     LOG.debug("Meter input={}", input);
146                 }
147             }
148
149             @Override
150             public void onFailure(Throwable throwable) {
151                 LOG.warn("Service call for removing meter={} failed", input.getMeterId(), throwable);
152             }
153         }, MoreExecutors.directExecutor());
154         return resultFuture;
155     }
156 }