2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services.sal;
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;
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;
41 private final DeviceContext deviceContext;
43 public SalMeterServiceImpl(final RequestContextStack requestContextStack,
44 final DeviceContext deviceContext,
45 final ConvertorExecutor convertorExecutor) {
46 this.deviceContext = deviceContext;
47 addMeter = new MultiLayerMeterService<>(requestContextStack,
52 updateMeter = new MultiLayerMeterService<>(requestContextStack,
54 UpdateMeterOutput.class,
57 removeMeter = new MultiLayerMeterService<>(requestContextStack,
59 RemoveMeterOutput.class,
62 addMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
63 updateMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
64 removeMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
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);
74 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
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());
82 if (LOG.isDebugEnabled()) {
83 LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
84 ErrorUtil.errorsToString(result.getErrors()));
90 public void onFailure(Throwable throwable) {
91 LOG.warn("Service call for adding meter={} failed", input.getMeterId(), throwable);
93 }, MoreExecutors.directExecutor());
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());
104 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
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());
113 LOG.warn("Meter update with id={} failed, errors={}", input.getOriginalMeter().getMeterId(),
114 ErrorUtil.errorsToString(result.getErrors()));
115 LOG.debug("Meter input={}", input.getUpdatedMeter());
120 public void onFailure(Throwable throwable) {
121 LOG.warn("Service call for updating meter={} failed",
122 input.getOriginalMeter().getMeterId(), throwable);
124 }, MoreExecutors.directExecutor());
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);
135 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
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());
143 LOG.warn("Meter remove with id={} failed, errors={}", input.getMeterId(),
144 ErrorUtil.errorsToString(result.getErrors()));
145 LOG.debug("Meter input={}", input);
150 public void onFailure(Throwable throwable) {
151 LOG.warn("Service call for removing meter={} failed", input.getMeterId(), throwable);
153 }, MoreExecutors.directExecutor());