Device state
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / MultipartRequestOnTheFlyCallback.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.base.Function;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
20 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
21 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
22 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
23 import org.opendaylight.openflowplugin.impl.statistics.SinglePurposeMultipartReplyTranslator;
24 import org.opendaylight.openflowplugin.impl.statistics.StatisticsGatheringUtils;
25 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.common.RpcError;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 final class MultipartRequestOnTheFlyCallback extends AbstractRequestCallback<List<MultipartReply>> {
38     private static final Logger LOG = LoggerFactory.getLogger(MultipartRequestOnTheFlyCallback.class);
39     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
40     private final DeviceInfo deviceInfo;
41     private final DeviceFlowRegistry registry;
42     private boolean virgin = true;
43     private boolean finished = false;
44     private final EventIdentifier doneEventIdentifier;
45     private final TxFacade txFacade;
46
47
48     public MultipartRequestOnTheFlyCallback(final RequestContext<List<MultipartReply>> context,
49                                             final Class<?> requestType,
50                                             final MessageSpy messageSpy,
51                                             final EventIdentifier eventIdentifier,
52                                             final DeviceInfo deviceInfo,
53                                             final DeviceFlowRegistry registry,
54                                             final TxFacade txFacade) {
55         super(context, requestType, messageSpy, eventIdentifier);
56
57         this.deviceInfo = deviceInfo;
58         this.registry = registry;
59         this.txFacade = txFacade;
60
61         //TODO: this is focused on flow stats only - need more general approach if used for more than flow stats
62         doneEventIdentifier = new EventIdentifier(MultipartType.OFPMPFLOW.name(), deviceInfo.getNodeId().toString());
63     }
64
65     public EventIdentifier getDoneEventIdentifier() {
66         return doneEventIdentifier;
67     }
68
69     @Override
70     public void onSuccess(final OfHeader result) {
71         if (result == null) {
72             LOG.info("Ofheader was null.");
73             if (!finished) {
74                 endCollecting();
75                 return;
76             }
77         } else if (finished) {
78             LOG.debug("Unexpected multipart response received: xid={}, {}", result.getXid(), result.getImplementedInterface());
79             return;
80         }
81
82         if (!(result instanceof MultipartReply)) {
83             LOG.info("Unexpected response type received {}.", result.getClass());
84             final RpcResultBuilder<List<MultipartReply>> rpcResultBuilder =
85                     RpcResultBuilder.<List<MultipartReply>>failed().withError(RpcError.ErrorType.APPLICATION,
86                             String.format("Unexpected response type received %s.", result.getClass()));
87             setResult(rpcResultBuilder.build());
88             endCollecting();
89         } else {
90             final MultipartReply multipartReply = (MultipartReply) result;
91
92             final MultipartReply singleReply = multipartReply;
93             final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(
94                     deviceInfo.getDatapathId(), deviceInfo.getVersion(), singleReply);
95             final Iterable<? extends DataObject> allMultipartData = multipartDataList;
96
97             //TODO: following part is focused on flow stats only - need more general approach if used for more than flow stats
98             ListenableFuture<Void> future;
99             if (virgin) {
100                 future = StatisticsGatheringUtils.deleteAllKnownFlows(deviceInfo, registry, txFacade);
101                 virgin = false;
102             } else {
103                 future = Futures.immediateFuture(null);
104             }
105
106             Futures.transform(future, new Function<Void, Void>() {
107
108                 @Override
109                 public Void apply(final Void input) {
110                     StatisticsGatheringUtils.writeFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData,
111                             deviceInfo, registry, txFacade);
112
113                     if (!multipartReply.getFlags().isOFPMPFREQMORE()) {
114                         endCollecting();
115                     }
116                     return input;
117                 }
118             });
119         }
120     }
121
122     private void endCollecting() {
123         EventsTimeCounter.markEnd(getDoneEventIdentifier());
124         EventsTimeCounter.markEnd(getEventIdentifier());
125         final RpcResult<List<MultipartReply>> rpcResult = RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build();
126         spyMessage(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
127         setResult(rpcResult);
128         txFacade.submitTransaction();
129         finished = true;
130     }
131 }