blob: 207cd2b95bb2c2565769e92f2f586f11082c17ea [file] [log] [blame]
Hyunsun Moon44aac662017-02-18 02:07:01 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon44aac662017-02-18 02:07:01 +09003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.openstacknetworking.impl;
17
18import com.google.common.collect.ImmutableSet;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onlab.util.Tools;
28import org.onosproject.event.ListenerRegistry;
29import org.onosproject.net.Host;
30import org.onosproject.net.HostId;
31import org.onosproject.net.host.HostEvent;
32import org.onosproject.net.host.HostListener;
33import org.onosproject.net.host.HostService;
34import org.onosproject.openstacknetworking.api.InstancePort;
35import org.onosproject.openstacknetworking.api.InstancePortEvent;
36import org.onosproject.openstacknetworking.api.InstancePortListener;
37import org.onosproject.openstacknetworking.api.InstancePortService;
38import org.slf4j.Logger;
39
40import java.util.Set;
41import java.util.stream.Collectors;
42
43import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_DETECTED;
44import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_UPDATED;
45import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_VANISHED;
46import static org.onosproject.openstacknetworking.impl.HostBasedInstancePort.ANNOTATION_NETWORK_ID;
47import static org.onosproject.openstacknetworking.impl.HostBasedInstancePort.ANNOTATION_PORT_ID;
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Provides implementation of administering and interfacing host based instance ports.
52 * It also provides instance port events for the hosts mapped to OpenStack VM interface.
53 */
54@Service
55@Component(immediate = true)
56public class HostBasedInstancePortManager
57 extends ListenerRegistry<InstancePortEvent, InstancePortListener>
58 implements InstancePortService {
59
60 protected final Logger log = getLogger(getClass());
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected HostService hostService;
64
65 private final HostListener hostListener = new InternalHostListener();
66
67 @Activate
68 protected void activate() {
69 hostService.addListener(hostListener);
70 log.info("Started");
71 }
72
73 @Deactivate
74 protected void deactivate() {
75 hostService.removeListener(hostListener);
76 log.info("Stopped");
77 }
78
79 @Override
80 public InstancePort instancePort(MacAddress macAddress) {
81 Host host = hostService.getHost(HostId.hostId(macAddress));
82 if (host == null || !isValidHost(host)) {
83 return null;
84 }
85 return HostBasedInstancePort.of(host);
86 }
87
88 @Override
89 public InstancePort instancePort(IpAddress ipAddress, String osNetId) {
90 return Tools.stream(hostService.getHosts()).filter(this::isValidHost)
91 .map(HostBasedInstancePort::of)
92 .filter(instPort -> instPort.networkId().equals(osNetId))
93 .filter(instPort -> instPort.ipAddress().equals(ipAddress))
94 .findAny().orElse(null);
95 }
96
97 @Override
98 public InstancePort instancePort(String osPortId) {
99 return Tools.stream(hostService.getHosts()).filter(this::isValidHost)
100 .map(HostBasedInstancePort::of)
101 .filter(instPort -> instPort.portId().equals(osPortId))
102 .findAny().orElse(null);
103 }
104
105 @Override
106 public Set<InstancePort> instancePorts() {
107 Set<InstancePort> instPors = Tools.stream(hostService.getHosts())
108 .filter(this::isValidHost)
109 .map(HostBasedInstancePort::of)
110 .collect(Collectors.toSet());
111 return ImmutableSet.copyOf(instPors);
112 }
113
114 @Override
115 public Set<InstancePort> instancePorts(String osNetId) {
116 Set<InstancePort> instPors = Tools.stream(hostService.getHosts())
117 .filter(this::isValidHost)
118 .map(HostBasedInstancePort::of)
119 .filter(instPort -> instPort.networkId().equals(osNetId))
120 .collect(Collectors.toSet());
121 return ImmutableSet.copyOf(instPors);
122 }
123
124 private boolean isValidHost(Host host) {
125 return !host.ipAddresses().isEmpty() &&
126 host.annotations().value(ANNOTATION_NETWORK_ID) != null &&
127 host.annotations().value(ANNOTATION_PORT_ID) != null;
128 }
129
130 private class InternalHostListener implements HostListener {
131
132 @Override
133 public boolean isRelevant(HostEvent event) {
134 Host host = event.subject();
135 if (!isValidHost(host)) {
136 log.debug("Invalid host detected, ignore it {}", host);
137 return false;
138 }
139 return true;
140 }
141
142 @Override
143 public void event(HostEvent event) {
144 InstancePort instPort = HostBasedInstancePort.of(event.subject());
145 InstancePortEvent instPortEvent;
146 switch (event.type()) {
147 case HOST_UPDATED:
148 instPortEvent = new InstancePortEvent(
149 OPENSTACK_INSTANCE_PORT_UPDATED,
150 instPort);
151 log.debug("Instance port is updated: {}", instPort);
152 process(instPortEvent);
153 break;
154 case HOST_ADDED:
155 instPortEvent = new InstancePortEvent(
156 OPENSTACK_INSTANCE_PORT_DETECTED,
157 instPort);
158 log.debug("Instance port is detected: {}", instPort);
159 process(instPortEvent);
160 break;
161 case HOST_REMOVED:
162 instPortEvent = new InstancePortEvent(
163 OPENSTACK_INSTANCE_PORT_VANISHED,
164 instPort);
165 log.debug("Instance port is disabled: {}", instPort);
166 process(instPortEvent);
167 break;
168 default:
169 break;
170 }
171 }
172 }
173}