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;
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 java.util.concurrent.Future;
14 import javax.annotation.Nullable;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
17 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
19 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource {
41 private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class);
42 private final MeterService<AddMeterInput, AddMeterOutput> addMeter;
43 private final MeterService<Meter, UpdateMeterOutput> updateMeter;
44 private final MeterService<RemoveMeterInput, RemoveMeterOutput> removeMeter;
45 private ItemLifecycleListener itemLifecycleListener;
46 private final DeviceContext deviceContext;
48 public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
49 this.deviceContext = deviceContext;
50 addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class, convertorExecutor);
51 updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class, convertorExecutor);
52 removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class, convertorExecutor);
56 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
57 this.itemLifecycleListener = itemLifecycleListener;
61 public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
62 final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
63 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
65 public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
66 if (result.isSuccessful()) {
67 if (LOG.isDebugEnabled()) {
68 LOG.debug("Meter add with id={} finished without error", input.getMeterId());
70 deviceContext.getDeviceMeterRegistry().store(input.getMeterId());
71 addIfNecessaryToDS(input.getMeterId(),input);
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
75 ErrorUtil.errorsToString(result.getErrors()));
81 public void onFailure(Throwable t) {
82 LOG.warn("Service call for adding meter={} failed, reason: {}", input.getMeterId(), t);
89 public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
90 final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
91 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
94 public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
95 if (result.isSuccessful()) {
96 if (LOG.isDebugEnabled()) {
97 LOG.debug("Meter update with id={} finished without error", input.getOriginalMeter().getMeterId());
99 if (itemLifecycleListener != null) {
100 removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
101 addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
104 if (LOG.isDebugEnabled()) {
105 LOG.debug("Meter update with id={} failed, errors={}", input.getOriginalMeter().getMeterId(),
106 ErrorUtil.errorsToString(result.getErrors()));
112 public void onFailure(Throwable t) {
113 LOG.warn("Service call for updating meter={} failed, reason: {}",
114 input.getOriginalMeter().getMeterId(),t);
121 public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
122 final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
123 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
125 public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
126 if (result.isSuccessful()) {
127 if (LOG.isDebugEnabled()) {
128 LOG.debug("Meter remove with id={} finished without error", input.getMeterId());
130 removeMeter.getDeviceRegistry().getDeviceMeterRegistry().addMark(input.getMeterId());
131 removeIfNecessaryFromDS(input.getMeterId());
133 if (LOG.isDebugEnabled()) {
134 LOG.debug("Meter remove with id={} failed, errors={}", input.getMeterId(),
135 ErrorUtil.errorsToString(result.getErrors()));
141 public void onFailure(Throwable t) {
142 LOG.warn("Service call for removing meter={} failed, reason: {}",input.getMeterId(),t);
148 private void removeIfNecessaryFromDS(final MeterId meterId) {
149 if (itemLifecycleListener != null) {
150 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
151 = createMeterPath(meterId, deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
152 itemLifecycleListener.onRemoved(meterPath);
156 private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
157 if (itemLifecycleListener != null) {
158 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
159 = createMeterPath(meterId, deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
160 itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
164 private static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
165 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));