002d58bdb6d4d0271d1369fe07d20a34c3d63a42
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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  */
16 package io.fd.honeycomb.v3po.translate.v3po.notification;
17
18 import com.google.common.base.Optional;
19 import com.google.common.collect.Lists;
20 import io.fd.honeycomb.v3po.notification.ManagedNotificationProducer;
21 import io.fd.honeycomb.v3po.notification.NotificationCollector;
22 import io.fd.honeycomb.v3po.translate.MappingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
24 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.concurrent.CompletionStage;
28 import java.util.concurrent.TimeoutException;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
31 import javax.annotation.concurrent.NotThreadSafe;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceDeleted;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceDeletedBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceNameOrIndex;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChange;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStateChangeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.InterfaceStatus;
38 import org.opendaylight.yangtools.yang.binding.Notification;
39 import org.openvpp.jvpp.VppBaseCallException;
40 import org.openvpp.jvpp.dto.SwInterfaceSetFlagsNotification;
41 import org.openvpp.jvpp.dto.WantInterfaceEvents;
42 import org.openvpp.jvpp.dto.WantInterfaceEventsReply;
43 import org.openvpp.jvpp.future.FutureJVpp;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Notification producer for interface events. It starts interface notification stream and for every
49  * received notification, it transforms it into its BA equivalent and pushes into HC's notification collector.
50  */
51 @NotThreadSafe
52 public final class InterfaceChangeNotificationProducer implements ManagedNotificationProducer {
53
54     private static final Logger LOG = LoggerFactory.getLogger(InterfaceChangeNotificationProducer.class);
55
56     private final FutureJVpp jvpp;
57     private final NamingContext interfaceContext;
58     private final MappingContext mappingContext;
59     @Nullable
60     private AutoCloseable notificationListenerReg;
61
62     public InterfaceChangeNotificationProducer(@Nonnull final FutureJVpp jvpp,
63                                                @Nonnull final NamingContext interfaceContext,
64                                                @Nonnull final MappingContext mappingContext) {
65         this.jvpp = jvpp;
66         this.interfaceContext = interfaceContext;
67         this.mappingContext = mappingContext;
68     }
69
70     @Override
71     public void start(final NotificationCollector collector) {
72         LOG.trace("Starting interface notifications");
73         enableDisableIfcNotifications(1);
74         LOG.debug("Interface notifications started successfully");
75         notificationListenerReg = jvpp.getNotificationRegistry().registerSwInterfaceSetFlagsNotificationCallback(
76             swInterfaceSetFlagsNotification -> {
77                 LOG.trace("Interface notification received: {}", swInterfaceSetFlagsNotification);
78                 // TODO this should be lazy
79                 collector.onNotification(transformNotification(swInterfaceSetFlagsNotification));
80             }
81         );
82     }
83
84     private Notification transformNotification(final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification) {
85         if (swInterfaceSetFlagsNotification.deleted == 1) {
86             return new InterfaceDeletedBuilder().setName(getIfcName(swInterfaceSetFlagsNotification)).build();
87         } else {
88             return new InterfaceStateChangeBuilder()
89                 .setName(getIfcName(swInterfaceSetFlagsNotification))
90                 .setAdminStatus(swInterfaceSetFlagsNotification.adminUpDown == 1 ? InterfaceStatus.Up : InterfaceStatus.Down)
91                 .setOperStatus(swInterfaceSetFlagsNotification.linkUpDown == 1 ? InterfaceStatus.Up : InterfaceStatus.Down)
92                 .build();
93         }
94     }
95
96     /**
97      * Get mapped name for the interface. Best effort only! The mapping might not yet be stored in context
98      * data tree (write transaction is still in progress and context changes have not been committed yet, or
99      * VPP sends the notification before it returns create request(that would store mapping)).
100      * <p/>
101      * In case mapping is not available, index is used as name. TODO inconsistent behavior, maybe just use indices ?
102      */
103     private InterfaceNameOrIndex getIfcName(final SwInterfaceSetFlagsNotification swInterfaceSetFlagsNotification) {
104         final Optional<String> optionalName =
105                 interfaceContext.getNameIfPresent(swInterfaceSetFlagsNotification.swIfIndex, mappingContext);
106         return optionalName.isPresent()
107             ? new InterfaceNameOrIndex(optionalName.get())
108             : new InterfaceNameOrIndex((long) swInterfaceSetFlagsNotification.swIfIndex);
109     }
110
111     @Override
112     public void stop() {
113         LOG.trace("Stopping interface notifications");
114         enableDisableIfcNotifications(0);
115         LOG.debug("Interface notifications stopped successfully");
116         try {
117             if (notificationListenerReg != null) {
118                 notificationListenerReg.close();
119             }
120         } catch (Exception e) {
121             LOG.warn("Unable to properly close notification registration: {}", notificationListenerReg, e);
122         }
123     }
124
125     private void enableDisableIfcNotifications(int enableDisable) {
126         final WantInterfaceEvents wantInterfaceEvents = new WantInterfaceEvents();
127         wantInterfaceEvents.pid = 1;
128         wantInterfaceEvents.enableDisable = enableDisable;
129         final CompletionStage<WantInterfaceEventsReply> wantInterfaceEventsReplyCompletionStage;
130         try {
131             wantInterfaceEventsReplyCompletionStage = jvpp.wantInterfaceEvents(wantInterfaceEvents);
132             TranslateUtils.getReply(wantInterfaceEventsReplyCompletionStage.toCompletableFuture());
133         } catch (VppBaseCallException | TimeoutException e) {
134             LOG.warn("Unable to {} interface notifications", enableDisable == 1 ? "enable" : "disable",  e);
135             throw new IllegalStateException("Unable to control interface notifications", e);
136         }
137     }
138
139     @Nonnull
140     @Override
141     public Collection<Class<? extends Notification>> getNotificationTypes() {
142         final ArrayList<Class<? extends Notification>> classes = Lists.newArrayList();
143         classes.add(InterfaceStateChange.class);
144         classes.add(InterfaceDeleted.class);
145         return classes;
146     }
147
148     @Override
149     public void close() throws Exception {
150         LOG.trace("Closing interface notifications producer");
151         stop();
152     }
153 }