Merge "Added JSON and XML payloads tabs with RFC 8040 URL"
[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.Optional;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
12 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
13 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemovedBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedFlowReason;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
21 import org.opendaylight.yangtools.yang.common.Uint8;
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
29         <FlowRemoved, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved> {
30     private final ConvertorExecutor convertorExecutor;
31     private static final Logger LOG = LoggerFactory.getLogger(FlowRemovedTranslator.class);
32
33     public FlowRemovedTranslator(final ConvertorExecutor convertorExecutor) {
34         this.convertorExecutor = convertorExecutor;
35     }
36
37     protected ConvertorExecutor getConvertorExecutor() {
38         return convertorExecutor;
39     }
40
41     @Override
42     public org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved
43             translate(final FlowRemoved input, final DeviceInfo deviceInfo, final Object connectionDistinguisher) {
44         FlowRemovedBuilder flowRemovedBld = new FlowRemovedBuilder()
45                 .setMatch(translateMatch(input, deviceInfo).build())
46                 .setCookie(new FlowCookie(input.getCookie()))
47                 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
48                 .setPriority(input.getPriority())
49                 .setTableId(translateTableId(input));
50
51         if (input.getReason() != null) {
52             flowRemovedBld.setReason(translateReason(input));
53         }
54
55         return flowRemovedBld.build();
56     }
57
58     protected MatchBuilder translateMatch(final FlowRemoved flowRemoved, final DeviceInfo deviceInfo) {
59         final VersionDatapathIdConvertorData datapathIdConvertorData =
60                 new VersionDatapathIdConvertorData(deviceInfo.getVersion());
61         datapathIdConvertorData.setDatapathId(deviceInfo.getDatapathId());
62
63         final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(
64                 flowRemoved.getMatch(),
65                 datapathIdConvertorData);
66
67         return matchBuilderOptional.orElse(new MatchBuilder());
68     }
69
70     /**
71      * Translate the table ID in the FLOW_REMOVED message to SAL table ID.
72      *
73      * @param flowRemoved  FLOW_REMOVED message.
74      * @return  SAL table ID.
75      */
76     protected Uint8 translateTableId(final FlowRemoved flowRemoved) {
77         return Uint8.valueOf(flowRemoved.getTableId().getValue());
78     }
79
80     private static RemovedFlowReason translateReason(final FlowRemoved removedFlow) {
81         LOG.debug("--Entering translateReason within FlowRemovedTranslator with reason: {}", removedFlow.getReason());
82         switch (removedFlow.getReason()) {
83             case OFPRRIDLETIMEOUT:
84                 return RemovedFlowReason.OFPRRIDLETIMEOUT;
85             case OFPRRHARDTIMEOUT:
86                 return RemovedFlowReason.OFPRRHARDTIMEOUT;
87             case OFPRRDELETE:
88                 return RemovedFlowReason.OFPRRDELETE;
89             case OFPRRGROUPDELETE:
90                 return RemovedFlowReason.OFPRRGROUPDELETE;
91             default:
92                 LOG.debug("The flow is being deleted for some unknown reason  ");
93                 return RemovedFlowReason.OFPRRDELETE;
94         }
95     }
96 }