eb9ff12296554045022cffdf270f6c08fc0be45f
[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.flow.MatchBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
20
21 /**
22  * translate {@link FlowRemoved} message to FlowRemoved notification (omit instructions)
23  */
24 public class FlowRemovedTranslator implements MessageTranslator<FlowRemoved, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved> {
25     private final ConvertorExecutor convertorExecutor;
26
27     public FlowRemovedTranslator(ConvertorExecutor convertorExecutor) {
28         this.convertorExecutor = convertorExecutor;
29     }
30
31     protected ConvertorExecutor getConvertorExecutor() {
32         return convertorExecutor;
33     }
34
35     @Override
36     public org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved translate(FlowRemoved input, DeviceInfo deviceInfo, Object connectionDistinguisher) {
37         FlowRemovedBuilder flowRemovedBld = new FlowRemovedBuilder()
38                 .setMatch(translateMatch(input, deviceInfo).build())
39                 .setCookie(new FlowCookie(input.getCookie()))
40                 .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier()))
41                 .setPriority(input.getPriority())
42                 .setTableId(translateTableId(input));
43
44         return flowRemovedBld.build();
45     }
46
47     protected MatchBuilder translateMatch(FlowRemoved flowRemoved, DeviceInfo deviceInfo) {
48         final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(deviceInfo.getVersion());
49         datapathIdConvertorData.setDatapathId(deviceInfo.getDatapathId());
50
51         final Optional<MatchBuilder> matchBuilderOptional = getConvertorExecutor().convert(
52                 flowRemoved.getMatch(),
53                 datapathIdConvertorData);
54
55         return matchBuilderOptional.orElse(new MatchBuilder());
56     }
57
58     /**
59      * Translate the table ID in the FLOW_REMOVED message to SAL table ID.
60      *
61      * @param flowRemoved  FLOW_REMOVED message.
62      * @return  SAL table ID.
63      */
64     protected Short translateTableId(FlowRemoved flowRemoved) {
65         return flowRemoved.getTableId().getValue().shortValue();
66     }
67 }