946669043a64676799b0c3ad28689bd403c3d086
[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.Collection;
11 import java.util.concurrent.Future;
12
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;
40
41 import javax.annotation.Nullable;
42
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;
50
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);
56     }
57
58     @Override
59     public void setItemLifecycleListener(@Nullable ItemLifecycleListener itemLifecycleListener) {
60         this.itemLifecycleListener = itemLifecycleListener;
61     }
62
63     @Override
64     public Future<RpcResult<AddMeterOutput>> addMeter(final AddMeterInput input) {
65         addMeter.getDeviceContext().getDeviceMeterRegistry().store(input.getMeterId());
66
67         final ListenableFuture<RpcResult<AddMeterOutput>> resultFuture = addMeter.handleServiceCall(input);
68         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<AddMeterOutput>>() {
69
70             @Override
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());
75                     }
76                     addIfNecessaryToDS(input.getMeterId(),input);
77                 }else{
78                     LOG.error("Meter add with id {} failed with error {}", input.getMeterId(),
79                             errorsToString(result.getErrors()));
80                 }
81             }
82
83             @Override
84             public void onFailure(Throwable t) {
85                 LOG.error("Meter add failed for id={}. Exception {}", input.getMeterId(), t);
86             }
87         });
88
89         return resultFuture;
90     }
91
92     @Override
93     public Future<RpcResult<UpdateMeterOutput>> updateMeter(final UpdateMeterInput input) {
94         final ListenableFuture<RpcResult<UpdateMeterOutput>> resultFuture = updateMeter.handleServiceCall(input.getUpdatedMeter());
95
96         Futures.addCallback(resultFuture, new FutureCallback<RpcResult<UpdateMeterOutput>>() {
97
98             @Override
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());
103                     }
104                     if (itemLifecycleListener != null) {
105                         removeIfNecessaryFromDS(input.getOriginalMeter().getMeterId());
106                         addIfNecessaryToDS(input.getUpdatedMeter().getMeterId(),input.getUpdatedMeter());
107                     }
108                 }else{
109                     LOG.error("Meter update with id {} failed with error {}", input.getOriginalMeter().getMeterId(),
110                             errorsToString(result.getErrors()));
111                 }
112             }
113
114             @Override
115             public void onFailure(Throwable t) {
116                 LOG.error("Service call for meter update failed. for id={}. Exception {}.",
117                         input.getOriginalMeter().getMeterId(),t);
118             }
119         });
120         return resultFuture;
121     }
122
123     @Override
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>>() {
128
129             @Override
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());
134                     }
135                     removeIfNecessaryFromDS(input.getMeterId());
136                 }else{
137                     LOG.error("Meter remove with id {} failed with error {}", input.getMeterId(),
138                             errorsToString(result.getErrors()));
139                 }
140             }
141
142             @Override
143             public void onFailure(Throwable t) {
144                 LOG.error("Service call for meter remove failed for id={}. Exception {}",input.getMeterId(),t);
145             }
146         });
147
148         return resultFuture;
149     }
150
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);
157         }
158     }
159
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());
166         }
167     }
168
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));
171     }
172
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());
178             }
179         }
180         return errors.toString();
181     }
182 }