Merge "BUG-4084: Li:Save meters in operational DS based on barrier success"
[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 java.util.concurrent.Future;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
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.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
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.RpcResult;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import javax.annotation.Nullable;
40
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;
48
49     public SalMeterServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
50         this.deviceContext = deviceContext;
51         addMeter = new MeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
52         updateMeter = new MeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
53         removeMeter = new MeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
54     }
55
56     @Override
57     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
58         this.itemLifecycleListener = itemLifecycleListener;
59     }
60
61     @Override
62     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
63         addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
64
65         final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
66         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
67
68             @Override
69             public void onSuccess(@Nullable RpcResult<AddMeterOutput> result) {
70                 if (result.isSuccessful()) {
71                     LOG.debug("Meter add finished without error, id={}", input.getMeterId());
72                     addIfNecessaryToDS(input.getMeterId(),input);
73                 }
74             }
75
76             @Override
77             public void onFailure(Throwable t) {
78                 LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
79             }
80         });
81
82         return resultFuture;
83     }
84
85     @Override
86     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
87         final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
88
89         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
90
91             @Override
92             public void onSuccess(@Nullable RpcResult<UpdateMeterOutput> result) {
93                 if (result.isSuccessful()) {
94                     LOG.debug("Meter update finished without error, id={}", input.getOriginalMeter().getMeterId());
95                     if (itemLifecycleListener != null) {
96                         removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
97                         addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
98                     }
99                 }
100             }
101
102             @Override
103             public void onFailure(Throwable t) {
104                 LOG.error("Meter update failed. for id={}. Exception {}.",input.getOriginalMeter().getMeterId(),t);
105             }
106         });
107         return resultFuture;
108     }
109
110     @Override
111     public Future<RpcResult<RemoveMeterOutput>> removeMeter(final RemoveMeterInput input) {
112         removeMeter.getDeviceContext().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
113         final ListenableFuture<RpcResult<RemoveMeterOutput>> resultFuture = removeMeter.handleServiceCall(input);
114         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
115
116             @Override
117             public void onSuccess(@Nullable RpcResult<RemoveMeterOutput> result) {
118                 if (result.isSuccessful()) {
119                     LOG.debug("Meter remove finished without error, id={}", input.getMeterId());
120                     removeIfNecessaryFromDS(input.getMeterId());
121                 }
122             }
123
124             @Override
125             public void onFailure(Throwable t) {
126                 LOG.error("Meter remove failed for id={}. Exception {}",input.getMeterId(),t);
127             }
128         });
129
130         return resultFuture;
131     }
132
133     private void removeIfNecessaryFromDS(final MeterId meterId) {
134         if (itemLifecycleListener != null) {
135             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> meterPath
136                     = createMeterPath(meterId,
137                     deviceContext.getDeviceState().getNodeInstanceIdentifier());
138             itemLifecycleListener.onRemoved(meterPath);
139         }
140     }
141
142     private void addIfNecessaryToDS(final MeterId meterId, final Meter data) {
143         if (itemLifecycleListener != null) {
144             KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> groupPath
145                     = createMeterPath(meterId,
146                     deviceContext.getDeviceState().getNodeInstanceIdentifier());
147             itemLifecycleListener.onAdded(groupPath, new MeterBuilder(data).build());
148         }
149     }
150
151     static KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter, MeterKey> createMeterPath(final MeterId meterId, final KeyedInstanceIdentifier<Node, NodeKey> nodePath) {
152         return nodePath.augmentation(FlowCapableNode.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter.class, new MeterKey(meterId));
153     }
154
155 }