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.Collection;
14 import java.util.concurrent.Future;
15 import javax.annotation.Nullable;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
19 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
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.RpcError;
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) {
49 this.deviceContext = deviceContext;
50 addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
51 updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
52 removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
56 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
57 this.itemLifecycleListener = itemLifecycleListener;
61 public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
62 addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
64 final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
65 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
68 public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
69 if (result.isSuccessful()) {
70 if(LOG.isDebugEnabled()) {
71 LOG.debug("Meter add finished without error, id={}", input.getMeterId());
73 addIfNecessaryToDS(input.getMeterId(),input);
75 LOG.error("Meter add with id {} failed with error {}", input.getMeterId(),
76 errorsToString(result.getErrors()));
81 public void onFailure(Throwable t) {
82 LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
90 public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
91 final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
93 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
96 public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
97 if (result.isSuccessful()) {
98 if(LOG.isDebugEnabled()) {
99 LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
101 if (itemLifecycleListener != null) {
102 removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
103 addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
106 LOG.error("Meter update with id {} failed with error {}", input.getOriginalMeter().getMeterId(),
107 errorsToString(result.getErrors()));
112 public void onFailure(Throwable t) {
113 LOG.error("Service call for meter update failed. for id={}. Exception {}.",
114 input.getOriginalMeter().getMeterId(),t);
121 public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
122 removeMeter.getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
123 final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
124 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
127 public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
128 if (result.isSuccessful()) {
129 if(LOG.isDebugEnabled()) {
130 LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
132 removeIfNecessaryFromDS(input.getMeterId());
134 LOG.error("Meter remove with id {} failed with error {}", input.getMeterId(),
135 errorsToString(result.getErrors()));
140 public void onFailure(Throwable t) {
141 LOG.error("Service call for meter remove failed for id={}. Exception {}",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,
152 deviceContext.getDeviceState().getNodeInstanceIdentifier());
153 itemLifecycleListener.onRemoved(meterPath);
157 private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
158 if (itemLifecycleListener != null) {
159 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
160 = createMeterPath(meterId,
161 deviceContext.getDeviceState().getNodeInstanceIdentifier());
162 itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
166 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
167 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
170 private final String errorsToString(final Collection<RpcError> rpcErrors) {
171 final StringBuilder errors = new StringBuilder();
172 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
173 for (final RpcError rpcError : rpcErrors) {
174 errors.append(rpcError.getMessage());
177 return errors.toString();