Java tutorial
/* * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.nemo.renderer.openflow.physicalnetwork; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class PhyConfigLoader { private static final Logger log = LoggerFactory.getLogger(PhyConfigLoader.class); public final static String NODE_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-node-resource.json"; public final static String LINK_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-link-resource.json"; public final static String EXTERNAL_NETWORK_MAC_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-external-resource.json"; public final static String HOST_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-host-resource.json"; private final static String NODES = "node"; private final static String NODE_ID = "node-id"; private final static String NODE_TYPE = "node-type"; private final static String PORTS = "port"; private final static String PORT_ID = "port-id"; private final static String PORT_TYPE = "port-type"; private final static String PORT_IP = "port-ip-address"; private final static String PORT_MAC = "port-mac-address"; private final static String LINKS = "link"; private final static String LINK_ID = "link-id"; private final static String SRC_NODE_ID = "left-node-id"; private final static String SRC_PORT_ID = "left-port-id"; private final static String DST_NODE_ID = "right-node-id"; private final static String DST_PORT_ID = "right-port-id"; private final static String BANDWIDTH = "link-bandwidth"; private final static String METRIC = "metric"; private final static String HOSTS = "host"; private final static String HOST_NAME = "name"; private final static String HOST_ID = "id"; private final static String HOST_IPS = "ip-addresses"; private final static String CONNECTOR_ID = "connector-id"; private final static String EXTERNAL_NETWORK_MAC = "external-network-mac"; private final static String IP_ADDRESS = "ip-address"; private final static String MAC_ADDRESS = "mac-address"; private final static String ATTRIBUTES = "attribute"; private final static String ATTRIBUTE_NAME = "name"; private final static String ATTRIBUTE_VALUE = "value"; protected final static long DEFAULT_LINK_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_DELAY = 1; protected final static short DEFAULT_LINK_LOSS_RATE = 1; private HashMap<PhysicalNodeId, PhysicalNode> physicalNodeHashMap; private HashMap<PhysicalPortId, PhysicalPort> physicalPortHashMap; private HashMap<PhysicalLinkId, PhysicalLink> physicalLinkHashMap; private Table<PhysicalNodeId, PhysicalPortId, MacAddress> externalNetworkMac; // final private DataBroker dataProvider; private ObjectMapper objectMapper = null; private DataBrokerAdapter dataBrokerAdapter = null; public PhyConfigLoader(DataBroker dataBroker) { physicalNodeHashMap = new HashMap<PhysicalNodeId, PhysicalNode>(); physicalPortHashMap = new HashMap<PhysicalPortId, PhysicalPort>(); physicalLinkHashMap = new HashMap<PhysicalLinkId, PhysicalLink>(); externalNetworkMac = HashBasedTable.create(); this.objectMapper = new ObjectMapper(); this.dataBrokerAdapter = new DataBrokerAdapter(dataBroker); loadNodesPorts(); loadLinks(); loadHosts(); loadExternals(); } public Table<PhysicalNodeId, PhysicalPortId, MacAddress> getExternalNetworkMac() { return externalNetworkMac; } protected PhysicalNode getPhysicalNode(PhysicalNodeId physicalNodeId) { return physicalNodeHashMap.get(physicalNodeId); } protected PhysicalPort getPhysicalPort(PhysicalPortId physicalPortId) { return physicalPortHashMap.get(physicalPortId); } protected PhysicalLink getPhysicalLink(PhysicalLinkId physicalLinkId) { return physicalLinkHashMap.get(physicalLinkId); } private void loadNodesPorts() { String nodesStr = Utils.readFile(NODE_PROPERTY); try { JsonNode nodeRoot = objectMapper.readTree(nodesStr); List<PhysicalNode> physicalNodes = buildNodes(nodeRoot); for (PhysicalNode physicalNode : physicalNodes) { physicalNodeHashMap.put(physicalNode.getNodeId(), physicalNode); for (PhysicalPort physicalPort : physicalNode.getPhysicalPort()) { physicalPortHashMap.put(physicalPort.getPortId(), physicalPort); } } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;", e); } } private void loadLinks() { String linkStr = Utils.readFile(LINK_PROPERTY); try { JsonNode linkRoot = objectMapper.readTree(linkStr); List<PhysicalLink> physicalLinks = buildLinks(linkRoot); for (PhysicalLink physicalLink : physicalLinks) { physicalLinkHashMap.put(physicalLink.getLinkId(), physicalLink); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;", e); } } private void loadExternals() { String externalStr = Utils.readFile(EXTERNAL_NETWORK_MAC_PROPERTY); try { JsonNode externalRoot = objectMapper.readTree(externalStr); buildExternals(externalRoot); } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;", e); } } private void loadHosts() { String hostStr = Utils.readFile(HOST_PROPERTY); try { JsonNode hostRoot = objectMapper.readTree(hostStr); List<PhysicalHost> physicalHosts = buildHosts(hostRoot); for (PhysicalHost physicalHost : physicalHosts) { dataBrokerAdapter.addPhysicalHost(physicalHost); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;", e); } } private void buildExternals(JsonNode externalRoot) { JsonNode exNetworkNodes = externalRoot.path(EXTERNAL_NETWORK_MAC); log.debug("Build external network mac : {} .", exNetworkNodes); for (int i = 0; i < exNetworkNodes.size(); i++) { log.debug("Build external network execution body"); JsonNode exNetworkNode = exNetworkNodes.get(i); buildExNetwork(exNetworkNode); } } private void buildExNetwork(JsonNode exNetwork) { String nodeId = exNetwork.get(NODE_ID).asText(); String portId = exNetwork.get(PORT_ID).asText(); String peerMac = exNetwork.get(MAC_ADDRESS).asText(); PhysicalNodeId accessNodeId = new PhysicalNodeId(nodeId); PhysicalPortId accessPortId = new PhysicalPortId(portId); MacAddress macAddress = new MacAddress(peerMac); externalNetworkMac.put(accessNodeId, accessPortId, macAddress); } private List<PhysicalNode> buildNodes(JsonNode nodesRoot) { List<PhysicalNode> physicalNodes = new ArrayList<PhysicalNode>(); JsonNode nodes = nodesRoot.path(NODES); log.debug("Build nodes: {} .", nodes); for (int i = 0; i < nodes.size(); i++) { log.debug("build physical node execution body"); JsonNode node = nodes.get(i); PhysicalNode phyNode = buildNode(node); if (phyNode != null) { physicalNodes.add(phyNode); } } return physicalNodes; } private PhysicalNode buildNode(JsonNode node) { PhysicalNodeBuilder nodeBuilder = new PhysicalNodeBuilder(); String node_id = node.get(NODE_ID).asText(); if (node_id.equals("")) return null; nodeBuilder.setNodeId(new PhysicalNodeId(node_id)); PhysicalNodeKey key = new PhysicalNodeKey(nodeBuilder.getNodeId()); nodeBuilder.setKey(key); String strType = node.get(NODE_TYPE).asText(); PhysicalNodeInstance.NodeType nodeType = Utils.getNodeType(strType); nodeBuilder.setNodeType(nodeType); JsonNode ports = node.path(PORTS); List<PhysicalPort> phyPortList = buildPorts(ports); nodeBuilder.setPhysicalPort(phyPortList); JsonNode attributes = node.path(ATTRIBUTES); nodeBuilder.setAttribute(buildNodeAttributes(attributes)); return nodeBuilder.build(); } private List<PhysicalPort> buildPorts(JsonNode ports) { List<PhysicalPort> phyPortList = new ArrayList<PhysicalPort>(); for (int j = 0; j < ports.size(); j++) { //JsonNode port = portIt.next(); JsonNode port = ports.get(j); PhysicalPort physicalPort = buildPort(port); if (physicalPort != null) { phyPortList.add(physicalPort); } } return phyPortList; } private PhysicalPort buildPort(JsonNode port) { PhysicalPortBuilder physicalPortBuilder = new PhysicalPortBuilder(); String strPortId = port.get(PORT_ID).asText(); physicalPortBuilder.setPortId(new PhysicalPortId(strPortId)); String strType = port.get(PORT_TYPE).asText(); PhysicalPortInstance.PortType portType = Utils.getPortType(strType); physicalPortBuilder.setPortType(portType); // String strMac = port.get(PORT_MAC).asText(); // MacAddress macAddress = new MacAddress(strMac); // physicalPortBuilder.setMacAddress(macAddress); // // long bandWidth = port.get(BANDWIDTH).asLong(); // physicalPortBuilder.setBandwidth(bandWidth); JsonNode portAttributes = port.path(ATTRIBUTES); List<Attribute> attributes = buildPortAttributes(portAttributes); physicalPortBuilder.setAttribute(attributes); return physicalPortBuilder.build(); } private List<Attribute> buildPortAttributes(JsonNode attributes) { List<Attribute> attributeList = new ArrayList<Attribute>(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); Attribute attribute = buildPortAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private Attribute buildPortAttribute(JsonNode attribute) { AttributeBuilder attributeBuilder = new AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute> buildNodeAttributes( JsonNode attributes) { List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute> attributeList = new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute>(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute attribute = buildNodeAttribute( portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute buildNodeAttribute( JsonNode attribute) { org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder attributeBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey( new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeKey( attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List<PhysicalLink> buildLinks(JsonNode linksRoot) { List<PhysicalLink> physicalLinks = new ArrayList<PhysicalLink>(); JsonNode links = linksRoot.path(LINKS); log.debug("Build links: {} .", links); for (int i = 0; i < links.size(); i++) { log.debug("build physical node execution body"); JsonNode link = links.get(i); PhysicalLink phyLink = buildLink(link); if (phyLink != null) { physicalLinks.add(phyLink); } } return physicalLinks; } private PhysicalLink buildLink(JsonNode linkNode) { PhysicalLinkBuilder linkBuilder = new PhysicalLinkBuilder(); String strLinkId = linkNode.get(LINK_ID).asText(); long metric = linkNode.get(METRIC).asLong(); linkBuilder.setLinkId(new PhysicalLinkId(strLinkId)); linkBuilder.setMetric(metric); linkBuilder.setBandwidth(DEFAULT_LINK_BANDWIDTH); linkBuilder.setDelay(DEFAULT_LINK_DELAY); linkBuilder.setLossRate(DEFAULT_LINK_LOSS_RATE); return linkBuilder.build(); } private List<PhysicalHost> buildHosts(JsonNode hostsNode) { List<PhysicalHost> physicalHosts = new ArrayList<PhysicalHost>(); JsonNode hosts = hostsNode.path(HOSTS); log.debug("Build hosts: {} .", hosts); for (int i = 0; i < hosts.size(); i++) { JsonNode host = hosts.get(i); PhysicalHost physicalHost = buildHost(host); if (physicalHost != null) { physicalHosts.add(physicalHost); } } return physicalHosts; } private PhysicalHost buildHost(JsonNode hostNode) { PhysicalHostBuilder hostBuilder = new PhysicalHostBuilder(); hostBuilder.setHostId(new PhysicalHostId(hostNode.get(HOST_ID).asText())); hostBuilder.setKey(new PhysicalHostKey(hostBuilder.getHostId())); hostBuilder.setHostName(new PhysicalHostName(hostNode.get(HOST_NAME).asText())); IpAddressesBuilder IpAddrBuilder = new IpAddressesBuilder(); List<IpAddress> ipList = new ArrayList<IpAddress>(); JsonNode ipaddrs = hostNode.path(HOST_IPS); for (int p = 0; p < ipaddrs.size(); p++) { JsonNode ipaddr = ipaddrs.get(p); IpAddress ip = new IpAddress(new Ipv4Address(ipaddr.get(IP_ADDRESS).asText())); ipList.add(ip); } IpAddrBuilder.setIpAddress(ipList); hostBuilder.setIpAddresses(IpAddrBuilder.build()); MacAddress mac = new MacAddress(hostNode.get(MAC_ADDRESS).asText()); hostBuilder.setMacAddress(mac); hostBuilder.setNodeId(new PhysicalNodeId(hostNode.get(NODE_ID).asText())); hostBuilder.setPortId(new PhysicalPortId(hostNode.get(CONNECTOR_ID).asText())); return hostBuilder.build(); } public void close() { physicalLinkHashMap.clear(); physicalPortHashMap.clear(); physicalNodeHashMap.clear(); externalNetworkMac.clear(); } }