Merge "Bug 3651: Fix creation of External EPG."
[groupbasedpolicy.git] / neutron-ovsdb / src / main / java / org / opendaylight / groupbasedpolicy / neutron / ovsdb / AbstractTunnelType.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
9 package org.opendaylight.groupbasedpolicy.neutron.ovsdb;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeBase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.Options;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.port._interface.attributes.OptionsBuilder;
23
24 public abstract class AbstractTunnelType {
25     protected static final String DESTPORT_KEY = "dst_port";
26     protected static final String REMOTE_IP_KEY = "remote_ip";
27     protected static final String REMOTE_IP_VALUE = "flow";
28     protected static final String VNID_KEY = "key";
29     protected static final String VNID_VALUE = "flow";
30
31     protected List<Options> createOptionsList(Map<String, String> optionsMap) {
32                 List<Options> options = new ArrayList<Options>();
33         OptionsBuilder ob = new OptionsBuilder();
34         for (Entry<String, String> entry: optionsMap.entrySet()) {
35             ob.setOption(entry.getKey());
36             ob.setValue(entry.getValue());
37             options.add(ob.build());
38         }
39         return Collections.unmodifiableList(options);
40         }
41
42     protected boolean hasTunnelOptions(OvsdbTerminationPointAugmentation tpAugmentation,
43             Map<String, String> optionsMap) {
44
45         Map<String, String> foundOpts = new HashMap<String, String>();
46         List<Options> options = tpAugmentation.getOptions();
47         if (options != null) {
48             for (Options opt: options) {
49                 // skip invalid options
50                 if (opt.getOption() == null
51                     || opt.getValue() == null) continue;
52
53                 if (optionsMap.containsKey(opt.getOption())
54                          && optionsMap.get(opt.getOption())
55                                    .equals(opt.getValue())) {
56                     foundOpts.put(opt.getOption(), opt.getValue());
57                 }
58             }
59             if ((foundOpts.size() == optionsMap.size())
60                     && (options.size() == foundOpts.size())) {
61                 return true;
62             }
63         }
64         return false;
65     }
66
67     protected String getDestPort(OvsdbTerminationPointAugmentation tpAugmentation) {
68         List<Options> options = tpAugmentation.getOptions();
69         if (options == null) {
70             return null;
71         }
72         for (Options opt: options) {
73             if (DESTPORT_KEY.equals(opt.getOption())) {
74                 return opt.getValue();
75             }
76         }
77         return null;
78     }
79
80     /**
81      * Return the {@link List<Options>} valid for this tunnel type
82      *
83      * @return {@link List<Options>} for the tunnel, null if not supported
84      */
85     public abstract List<Options> getOptions();
86
87     /**
88      * Check if a TerminationPoint is a tunnel port that meets
89      * requirements
90      *
91      * @param tpAugmentation
92      * @return String of the tunnel port name (null if not found)
93      */
94     public abstract boolean isValidTunnelPort(OvsdbTerminationPointAugmentation tpAugmentation);
95
96     /**
97      * Return the type of tunnel.
98      *
99      * @return type of tunnel
100      */
101     public abstract Class<? extends TunnelTypeBase> getTunnelType();
102
103     /**
104      * Some {@link AbstractTunnelType} objects have a UDP port property.
105      * This getter method applies to those ports.
106      *
107      * @return {@link PortNumber} if the {@link AbstractTunnelType} supports it, null otherwise
108      */
109     public abstract PortNumber getPortNumber();
110
111     /**
112      * Get the prefix used to create the tunnel name for any tunnels of this type
113      *
114      * @return The tunnel prefix
115      */
116     public abstract String getTunnelPrefix();
117 }