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.Collection;
11 import java.util.concurrent.Future;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
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.group.types.rev131018.groups.GroupBuilder;
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 import javax.annotation.Nullable;
43 public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource {
44 private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class);
45 private final MeterService<AddMeterInput, AddMeterOutput> addMeter;
46 private final MeterService<Meter, UpdateMeterOutput> updateMeter;
47 private final MeterService<RemoveMeterInput, RemoveMeterOutput> removeMeter;
48 private ItemLifecycleListener itemLifecycleListener;
49 private final DeviceContext deviceContext;
51 public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
52 this.deviceContext = deviceContext;
53 addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
54 updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
55 removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
59 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
60 this.itemLifecycleListener = itemLifecycleListener;
64 public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
65 addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
67 final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
68 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
71 public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
72 if (result.isSuccessful()) {
73 if(LOG.isDebugEnabled()) {
74 LOG.debug("Meter add finished without error, id={}", input.getMeterId());
76 addIfNecessaryToDS(input.getMeterId(),input);
78 LOG.error("Meter add with id {} failed with error {}", input.getMeterId(),
79 errorsToString(result.getErrors()));
84 public void onFailure(Throwable t) {
85 LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
93 public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
94 final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
96 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
99 public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
100 if (result.isSuccessful()) {
101 if(LOG.isDebugEnabled()) {
102 LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
104 if (itemLifecycleListener != null) {
105 removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
106 addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
109 LOG.error("Meter update with id {} failed with error {}", input.getOriginalMeter().getMeterId(),
110 errorsToString(result.getErrors()));
115 public void onFailure(Throwable t) {
116 LOG.error("Service call for meter update failed. for id={}. Exception {}.",
117 input.getOriginalMeter().getMeterId(),t);
124 public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
125 removeMeter.getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
126 final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
127 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
130 public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
131 if (result.isSuccessful()) {
132 if(LOG.isDebugEnabled()) {
133 LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
135 removeIfNecessaryFromDS(input.getMeterId());
137 LOG.error("Meter remove with id {} failed with error {}", input.getMeterId(),
138 errorsToString(result.getErrors()));
143 public void onFailure(Throwable t) {
144 LOG.error("Service call for meter remove failed for id={}. Exception {}",input.getMeterId(),t);
151 private void removeIfNecessaryFromDS(final MeterId meterId) {
152 if (itemLifecycleListener != null) {
153 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
154 = createMeterPath(meterId,
155 deviceContext.getDeviceState().getNodeInstanceIdentifier());
156 itemLifecycleListener.onRemoved(meterPath);
160 private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
161 if (itemLifecycleListener != null) {
162 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
163 = createMeterPath(meterId,
164 deviceContext.getDeviceState().getNodeInstanceIdentifier());
165 itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
169 static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
170 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
173 private final String errorsToString(final Collection<RpcError> rpcErrors) {
174 final StringBuilder errors = new StringBuilder();
175 if ((null != rpcErrors) && (rpcErrors.size() > 0)) {
176 for (final RpcError rpcError : rpcErrors) {
177 errors.append(rpcError.getMessage());
180 return errors.toString();