Bug 7453 - FlowRemoved doesn't have Removed Reason Information
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / translator / FlowRemovedTranslator.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.translator;
9
10 import java.util.Objects;
11 import java.util.Optional;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
13 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemovedBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedFlowReason;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * translate {@link FlowRemoved} message to FlowRemoved notification (omit instructions)
27  */
28 public class FlowRemovedTranslator implements MessageTranslator<FlowRemoved, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved> {
29     private final ConvertorExecutor convertorExecutor;
30     private static final Logger logger = LoggerFactory.getLogger(FlowRemovedTranslator.class);
31     public FlowRemovedTranslator(ConvertorExecutor convertorExecutor) {
32         this.convertorExecutor = convertorExecutor;
33     }
34
35     protected ConvertorExecutor getConvertorExecutor() {
36         return convertorExecutor;
37     }
38
39     @Override
40     public org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved translate(FlowRemoved input, DeviceInfo deviceInfo, Object connectionDistinguisher) {
41         FlowRemovedBuilder flowRemovedBld = new FlowRemovedBuilder()
42                 .setMatch(translateMatch(input, deviceInfo).build())
43                 .setCookie(new FlowCookie(input.getCookie()))
44                 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
45                 .setPriority(input.getPriority())
46                 .setTableId(translateTableId(input));
47
48         if(Objects.nonNull(input.getReason())) {
49             flowRemovedBld.setReason(translateReason(input));
50         }
51
52         return flowRemovedBld.build();
53     }
54
55     protected MatchBuilder translateMatch(FlowRemoved flowRemoved, DeviceInfo deviceInfo) {
56         final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(deviceInfo.getVersion());
57         datapathIdConvertorData.setDatapathId(deviceInfo.getDatapathId());
58
59         final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(
60                 flowRemoved.getMatch(),
61                 datapathIdConvertorData);
62
63         return matchBuilderOptional.orElse(new MatchBuilder());
64     }
65
66     /**
67      * Translate the table ID in the FLOW_REMOVED message to SAL table ID.
68      *
69      * @param flowRemoved  FLOW_REMOVED message.
70      * @return  SAL table ID.
71      */
72     protected Short translateTableId(FlowRemoved flowRemoved) {
73         return flowRemoved.getTableId().getValue().shortValue();
74     }
75
76
77     private RemovedFlowReason translateReason(FlowRemoved removedFlow) {
78         logger.debug("--Entering translateReason within FlowRemovedTranslator with reason:{} " + removedFlow.getReason());
79         switch (removedFlow.getReason()) {
80             case OFPRRIDLETIMEOUT:
81                 return RemovedFlowReason.OFPRRIDLETIMEOUT;
82             case OFPRRHARDTIMEOUT:
83                 return RemovedFlowReason.OFPRRHARDTIMEOUT;
84             case OFPRRDELETE:
85                 return RemovedFlowReason.OFPRRDELETE;
86             case OFPRRGROUPDELETE:
87                 return RemovedFlowReason.OFPRRGROUPDELETE;
88             default:
89                 logger.debug("The flow is being deleted for some unknown reason  ");
90                 return RemovedFlowReason.OFPRRDELETE;
91         }
92     }
93 }