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.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.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
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, final ConvertorExecutor convertorExecutor) {
50 this.deviceContext = deviceContext;
51 addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class, convertorExecutor);
52 updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class, convertorExecutor);
53 removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class, convertorExecutor);
57 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
58 this.itemLifecycleListener = itemLifecycleListener;
62 public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
63 addMeter.getDeviceRegistry().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 if(LOG.isDebugEnabled()) {
72 LOG.debug("Meter add finished without error, id={}", input.getMeterId());
74 addIfNecessaryToDS(input.getMeterId(),input);
76 LOG.error("Meter add with id {} failed with error {}", input.getMeterId(),
77 errorsToString(result.getErrors()));
82 public void onFailure(Throwable t) {
83 LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
91 public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
92 final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
94 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
97 public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
98 if (result.isSuccessful()) {
99 if(LOG.isDebugEnabled()) {
100 LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
102 if (itemLifecycleListener != null) {
103 removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
104 addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
107 LOG.error("Meter update with id {} failed with error {}", input.getOriginalMeter().getMeterId(),
108 errorsToString(result.getErrors()));
113 public void onFailure(Throwable t) {
114 LOG.error("Service call for meter update failed. for id={}. Exception {}.",
115 input.getOriginalMeter().getMeterId(),t);
122 public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
123 removeMeter.getDeviceRegistry().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
124 final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
125 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
128 public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
129 if (result.isSuccessful()) {
130 if(LOG.isDebugEnabled()) {
131 LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
133 removeIfNecessaryFromDS(input.getMeterId());
135 LOG.error("Meter remove with id {} failed with error {}", input.getMeterId(),
136 errorsToString(result.getErrors()));
141 public void onFailure(Throwable t) {
142 LOG.error("Service call for meter remove failed for id={}. Exception {}",input.getMeterId(),t);
149 private void removeIfNecessaryFromDS(final MeterId meterId) {
150 if (itemLifecycleListener != null) {
151 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
152 = createMeterPath(meterId,
153 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
154 itemLifecycleListener.onRemoved(meterPath);
158 private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
159 if (itemLifecycleListener != null) {
160 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
161 = createMeterPath(meterId,
162 deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
163 itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
167 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
168 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
171 private final String errorsToString(final Collection<RpcError> rpcErrors) {
172 final StringBuilder errors = new StringBuilder();
173 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
174 for (final RpcError rpcError : rpcErrors) {
175 errors.append(rpcError.getMessage());
178 return errors.toString();