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.sal;
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.services.singlelayer.SingleLayerMeterService;
20 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerMeterService;
21 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.RemoveMeterOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.UpdateMeterOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.Meter;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class SalMeterServiceImpl implements SalMeterService, ItemLifeCycleSource {
43 private static final Logger LOG = LoggerFactory.getLogger(SalMeterServiceImpl.class);
44 private final MultiLayerMeterService<AddMeterInput, AddMeterOutput> addMeter;
45 private final MultiLayerMeterService<Meter, UpdateMeterOutput> updateMeter;
46 private final MultiLayerMeterService<RemoveMeterInput, RemoveMeterOutput> removeMeter;
47 private final SingleLayerMeterService<AddMeterOutput> addMeterMessage;
48 private final SingleLayerMeterService<UpdateMeterOutput> updateMeterMessage;
49 private final SingleLayerMeterService<RemoveMeterOutput> removeMeterMessage;
51 private ItemLifecycleListener itemLifecycleListener;
52 private final DeviceContext deviceContext;
54 public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
55 this.deviceContext = deviceContext;
56 addMeter = new MultiLayerMeterService<>(requestContextStack, deviceContext, AddMeterOutput.class, convertorExecutor);
57 updateMeter = new MultiLayerMeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class, convertorExecutor);
58 removeMeter = new MultiLayerMeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class, convertorExecutor);
60 addMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
61 updateMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
62 removeMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
66 public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
67 this.itemLifecycleListener = itemLifecycleListener;
71 public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
72 final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeterMessage.canUseSingleLayerSerialization()
73 ? addMeterMessage.handleServiceCall(input)
74 : addMeter.handleServiceCall(input);
76 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
78 public void onSuccess(RpcResult<AddMeterOutput> result) {
79 if (result.isSuccessful()) {
80 if (LOG.isDebugEnabled()) {
81 LOG.debug("Meter add with id={} finished without error", input.getMeterId());
83 deviceContext.getDeviceMeterRegistry().store(input.getMeterId());
84 addIfNecessaryToDS(input.getMeterId(),input);
86 if (LOG.isDebugEnabled()) {
87 LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
88 ErrorUtil.errorsToString(result.getErrors()));
94 public void onFailure(Throwable t) {
95 LOG.warn("Service call for adding meter={} failed, reason: {}", input.getMeterId(), t);
102 public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
103 final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeterMessage.canUseSingleLayerSerialization()
104 ? updateMeterMessage.handleServiceCall(input.getUpdatedMeter())
105 : updateMeter.handleServiceCall(input.getUpdatedMeter());
107 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
110 public void onSuccess(RpcResult<UpdateMeterOutput> result) {
111 if (result.isSuccessful()) {
112 if (LOG.isDebugEnabled()) {
113 LOG.debug("Meter update with id={} finished without error", input.getOriginalMeter().getMeterId());
115 if (itemLifecycleListener != null) {
116 removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
117 addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
120 LOG.warn("Meter update with id={} failed, errors={}", input.getOriginalMeter().getMeterId(),
121 ErrorUtil.errorsToString(result.getErrors()));
122 LOG.debug("Meter input={}", input.getUpdatedMeter());
127 public void onFailure(Throwable t) {
128 LOG.warn("Service call for updating meter={} failed, reason: {}",
129 input.getOriginalMeter().getMeterId(),t);
136 public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
137 final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeterMessage.canUseSingleLayerSerialization()
138 ? removeMeterMessage.handleServiceCall(input)
139 : removeMeter.handleServiceCall(input);
141 Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
143 public void onSuccess(RpcResult<RemoveMeterOutput> result) {
144 if (result.isSuccessful()) {
145 if (LOG.isDebugEnabled()) {
146 LOG.debug("Meter remove with id={} finished without error", input.getMeterId());
148 removeMeter.getDeviceRegistry().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
149 removeIfNecessaryFromDS(input.getMeterId());
151 LOG.warn("Meter remove with id={} failed, errors={}", input.getMeterId(),
152 ErrorUtil.errorsToString(result.getErrors()));
153 LOG.debug("Meter input={}", input);
158 public void onFailure(Throwable t) {
159 LOG.warn("Service call for removing meter={} failed, reason: {}",input.getMeterId(),t);
165 private void removeIfNecessaryFromDS(final MeterId meterId) {
166 if (itemLifecycleListener != null) {
167 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
168 = createMeterPath(meterId, deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
169 itemLifecycleListener.onRemoved(meterPath);
173 private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
174 if (itemLifecycleListener != null) {
175 KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
176 = createMeterPath(meterId, deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
177 itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
181 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) {
182 return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));