02f72dc6ab8195da6c55f43f32682d7580ca5960
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / SalMeterServiceImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
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.util.ErrorUtil;
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.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
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;
47
48     public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
49         this.deviceContext = deviceContext;
50         addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class, convertorExecutor);
51         updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class, convertorExecutor);
52         removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class, convertorExecutor);
53     }
54
55     @Override
56     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
57         this.itemLifecycleListener = itemLifecycleListener;
58     }
59
60     @Override
61     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
62         final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
63         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
64             @Override
65             public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
66                 if (result.isSuccessful()) {
67                    if (LOG.isDebugEnabled()) {
68                         LOG.debug("Meter add with id={} finished without error", input.getMeterId());
69                     }
70                     deviceContext.getDeviceMeterRegistry().store(input.getMeterId());
71                     addIfNecessaryToDS(input.getMeterId(),input);
72                 } else {
73                     if (LOG.isDebugEnabled()) {
74                         LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
75                                 ErrorUtil.errorsToString(result.getErrors()));
76                     }
77                 }
78             }
79
80             @Override
81             public void onFailure(Throwable t) {
82                  LOG.warn("Service call for adding meter={} failed, reason: {}", input.getMeterId(), t);
83             }
84         });
85         return resultFuture;
86     }
87
88     @Override
89     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
90         final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
91         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
92
93             @Override
94             public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
95                 if (result.isSuccessful()) {
96                     if (LOG.isDebugEnabled()) {
97                         LOG.debug("Meter update with id={} finished without error", input.getOriginalMeter().getMeterId());
98                     }
99                     if (itemLifecycleListener != null) {
100                         removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
101                         addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
102                     }
103                 } else {
104                     if (LOG.isDebugEnabled()) {
105                         LOG.debug("Meter update with id={} failed, errors={}", input.getOriginalMeter().getMeterId(),
106                                 ErrorUtil.errorsToString(result.getErrors()));
107                     }
108                 }
109             }
110
111             @Override
112             public void onFailure(Throwable t) {
113                 LOG.warn("Service call for updating meter={} failed, reason: {}",
114                         input.getOriginalMeter().getMeterId(),t);
115             }
116         });
117         return resultFuture;
118     }
119
120     @Override
121     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
122         removeMeter.getDeviceRegistry().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
123         final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
124         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
125             @Override
126             public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
127                 if (result.isSuccessful()) {
128                     if (LOG.isDebugEnabled()) {
129                         LOG.debug("Meter remove with id={} finished without error", input.getMeterId());
130                     }
131                     removeIfNecessaryFromDS(input.getMeterId());
132                 } else {
133                     if (LOG.isDebugEnabled()) {
134                         LOG.debug("Meter remove with id={} failed, errors={}", input.getMeterId(),
135                                 ErrorUtil.errorsToString(result.getErrors()));
136                     }
137                 }
138             }
139
140             @Override
141             public void onFailure(Throwable t) {
142                 LOG.warn("Service call for removing meter={} failed, reason: {}",input.getMeterId(),t);
143             }
144         });
145         return resultFuture;
146     }
147
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, deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
152             itemLifecycleListener.onRemoved(meterPath);
153         }
154     }
155
156     private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
157         if (itemLifecycleListener != null) {
158             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
159                     = createMeterPath(meterId, deviceContext.getDeviceInfo().getNodeInstanceIdentifier());
160             itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
161         }
162     }
163
164     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) {
165         return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
166     }
167 }