Add single layer deserialization support
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / 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.sal;
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.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;
41
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;
50
51     private ItemLifecycleListener itemLifecycleListener;
52     private final DeviceContext deviceContext;
53
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);
59
60         addMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, AddMeterOutput.class);
61         updateMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, UpdateMeterOutput.class);
62         removeMeterMessage = new SingleLayerMeterService<>(requestContextStack, deviceContext, RemoveMeterOutput.class);
63     }
64
65     @Override
66     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
67         this.itemLifecycleListener = itemLifecycleListener;
68     }
69
70     @Override
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);
75
76         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
77             @Override
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());
82                     }
83                     deviceContext.getDeviceMeterRegistry().store(input.getMeterId());
84                     addIfNecessaryToDS(input.getMeterId(),input);
85                 } else {
86                     if (LOG.isDebugEnabled()) {
87                         LOG.debug("Meter add with id={} failed, errors={}", input.getMeterId(),
88                             ErrorUtil.errorsToString(result.getErrors()));
89                     }
90                 }
91             }
92
93             @Override
94             public void onFailure(Throwable t) {
95                  LOG.warn("Service call for adding meter={} failed, reason: {}", input.getMeterId(), t);
96             }
97         });
98         return resultFuture;
99     }
100
101     @Override
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());
106
107         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
108
109             @Override
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());
114                     }
115                     if (itemLifecycleListener != null) {
116                         removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
117                         addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
118                     }
119                 } else {
120                         LOG.warn("Meter update with id={} failed, errors={}", input.getOriginalMeter().getMeterId(),
121                                 ErrorUtil.errorsToString(result.getErrors()));
122                         LOG.debug("Meter input={}", input.getUpdatedMeter());
123                 }
124             }
125
126             @Override
127             public void onFailure(Throwable t) {
128                 LOG.warn("Service call for updating meter={} failed, reason: {}",
129                         input.getOriginalMeter().getMeterId(),t);
130             }
131         });
132         return resultFuture;
133     }
134
135     @Override
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);
140
141         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<RemoveMeterOutput>>() {
142             @Override
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());
147                     }
148                     removeMeter.getDeviceRegistry().getDeviceMeterRegistry().markToBeremoved(input.getMeterId());
149                     removeIfNecessaryFromDS(input.getMeterId());
150                 } else {
151                     LOG.warn("Meter remove with id={} failed, errors={}", input.getMeterId(),
152                         ErrorUtil.errorsToString(result.getErrors()));
153                     LOG.debug("Meter input={}", input);
154                 }
155             }
156
157             @Override
158             public void onFailure(Throwable t) {
159                 LOG.warn("Service call for removing meter={} failed, reason: {}",input.getMeterId(),t);
160             }
161         });
162         return resultFuture;
163     }
164
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);
170         }
171     }
172
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());
178         }
179     }
180
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));
183     }
184 }