aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/PktLinkConfig.java
blob: 3c51fa9dcf6957b178ef6cf2dfa2ebfad70e683d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.segmentrouting.config;

import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.onosproject.net.Link;
import org.onosproject.segmentrouting.config.NetworkConfig.LinkConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * Reserved for future use.
 * Configuration for a link between two packet-switches.
 */
public class PktLinkConfig extends LinkConfig {
    protected static final Logger log = LoggerFactory
            .getLogger(PktLinkConfig.class);
    private int port1;
    private int port2;
    private String nodeName1;
    private String nodeName2;
    private List<Link> linkTupleList;

    public PktLinkConfig(LinkConfig lkc) {
        nodeDpid1 = lkc.getNodeDpid1();
        nodeDpid2 = lkc.getNodeDpid2();
        dpid1 = lkc.getDpid1();
        dpid2 = lkc.getDpid2();
        type = lkc.getType();
        allowed = lkc.isAllowed();
        params = lkc.getParams();
        publishAttributes = new ConcurrentHashMap<>();
        parseParams();
        validateParams();
        setPublishAttributes();
    }

    // ********************
    // Packet Link Configuration
    // ********************

    public int getPort1() {
        return port1;
    }

    public void setPort1(int port1) {
        this.port1 = port1;
    }

    public int getPort2() {
        return port2;
    }

    public void setPort2(int port2) {
        this.port2 = port2;
    }

    public String getNodeName1() {
        return nodeName1;
    }

    public void setNodeName1(String nodeName1) {
        this.nodeName1 = nodeName1;
    }

    public String getNodeName2() {
        return nodeName2;
    }

    public void setNodeName2(String nodeName2) {
        this.nodeName2 = nodeName2;
    }

    /**
     * Returns the two unidirectional links corresponding to the packet-link
     * configuration. It is possible that the ports in the LinkTuple have
     * portnumber '0', implying that the configuration applies to all links
     * between the two switches.
     *
     * @return a list of LinkTuple with exactly 2 unidirectional links
     */
    public List<Link> getLinkTupleList() {
        return linkTupleList;
    }

    private void setPublishAttributes() {

    }

    private void parseParams() {
        if (params == null) {
            throw new PktLinkParamsNotSpecified(nodeDpid1, nodeDpid2);
        }
        Set<Entry<String, JsonNode>> m = params.entrySet();
        for (Entry<String, JsonNode> e : m) {
            String key = e.getKey();
            JsonNode j = e.getValue();
            if (key.equals("nodeName1")) {
                setNodeName1(j.asText());
            } else if (key.equals("nodeName2")) {
                setNodeName2(j.asText());
            } else if (key.equals("port1")) {
                setPort1(j.asInt());
            } else if (key.equals("port2")) {
                setPort2(j.asInt());
            } else {
                throw new UnknownPktLinkConfig(key, nodeDpid1, nodeDpid2);
            }
        }
    }

    private void validateParams() {
        // TODO - wrong-names, duplicate links,
        // duplicate use of port, is switch-allowed for which link is allowed?
        // valid port numbers
    }

    public static class PktLinkParamsNotSpecified extends RuntimeException {
        private static final long serialVersionUID = 6247582323691265513L;

        public PktLinkParamsNotSpecified(String dpidA, String dpidB) {
            super();
            log.error("Params required for packet link - not specified "
                    + "for link between switch1:{} and switch2:{}",
                    dpidA, dpidB);
        }
    }

    public static class UnknownPktLinkConfig extends RuntimeException {
        private static final long serialVersionUID = -5750132094884129179L;

        public UnknownPktLinkConfig(String key, String dpidA, String dpidB) {
            super();
            log.error("Unknown packet-link config {} for link between"
                    + " dpid1: {} and dpid2: {}", key,
                    dpidA, dpidB);
        }
    }

}