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 java.util.concurrent.Future;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import javax.annotation.Nullable;
41 public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource {
42 private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class);
43 private final MeterService<AddMeterInput, AddMeterOutput> addMeter;
44 private final MeterService<Meter, UpdateMeterOutput> updateMeter;
45 private final MeterService<RemoveMeterInput, RemoveMeterOutput> removeMeter;
46 private ItemLifecycleListener itemLifecycleListener;
47 private final DeviceContext deviceContext;
49 public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
50 this.deviceContext = deviceContext;
51 addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
52 updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
53 removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
57 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
58 this.itemLifecycleListener = itemLifecycleListener;
62 public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
63 addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
65 final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
66 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
69 public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
70 if (result.isSuccessful()) {
71 LOG.debug("Meter add finished without error, id={}", input.getMeterId());
72 addIfNecessaryToDS(input.getMeterId(),input);
77 public void onFailure(Throwable t) {
78 LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
86 public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
87 final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
89 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
92 public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
93 if (result.isSuccessful()) {
94 LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
95 if (itemLifecycleListener != null) {
96 removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
97 addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
103 public void onFailure(Throwable t) {
104 LOG.error("Meter update failed. for id={}. Exception {}.",input.getOriginalMeter().getMeterId(),t);
111 public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
112 removeMeter.getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
113 final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
114 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
117 public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
118 if (result.isSuccessful()) {
119 LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
120 removeIfNecessaryFromDS(input.getMeterId());
125 public void onFailure(Throwable t) {
126 LOG.error("Meter remove failed for id={}. Exception {}",input.getMeterId(),t);
133 private void removeIfNecessaryFromDS(final MeterId meterId) {
134 if (itemLifecycleListener != null) {
135 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
136 = createMeterPath(meterId,
137 deviceContext.getDeviceState().getNodeInstanceIdentifier());
138 itemLifecycleListener.onRemoved(meterPath);
142 private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
143 if (itemLifecycleListener != null) {
144 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
145 = createMeterPath(meterId,
146 deviceContext.getDeviceState().getNodeInstanceIdentifier());
147 itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
151 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
152 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));