minor clean-up after previous change
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / impl / PortStatusUpdateInitializer.java
1 /*
2  * Copyright (c) 2018 Red Hat, 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 package org.opendaylight.neutron.northbound.impl;
9
10 import com.google.common.base.Optional;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import javax.ws.rs.core.MultivaluedHashMap;
14
15 import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.northbound.api.config.rev181024.NeutronNorthboundApiConfig;
17 import org.opendaylight.yangtools.yang.common.OperationFailedException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 @Singleton
22 public class PortStatusUpdateInitializer {
23     private static final Logger LOG = LoggerFactory.getLogger(PortStatusUpdateInitializer.class);
24
25     private static final String SUBSCRIBE_JSON = "{\n"
26             + "  \"input\": {\n"
27             + "    \"path\": \"/neutron:neutron/neutron:ports\", \n"
28             + "    \"sal-remote-augment:notification-output-type\": \"JSON\", \n"
29             + "    \"sal-remote-augment:datastore\": \"OPERATIONAL\", \n"
30             + "    \"sal-remote-augment:scope\": \"SUBTREE\"\n"
31             + "  }\n"
32             + "}\n";
33
34     private final NeutronNorthboundApiConfig cfg;
35     private final JSONRestconfService jsonRestconfService;
36
37     @Inject
38     public PortStatusUpdateInitializer(JSONRestconfService jsonRestconfService,
39                                        final NeutronNorthboundApiConfig neutronNorthboundApiConfig) {
40         this.cfg = neutronNorthboundApiConfig;
41         this.jsonRestconfService = jsonRestconfService;
42
43         boolean preRegister = cfg.isPreRegisterPortStatusWebsocket() == null || cfg.isPreRegisterPortStatusWebsocket();
44
45         if (preRegister) {
46             subscribeWebsocket();
47         } else {
48             LOG.info("PortStatusUpdateInitializer: Skipping pre-register of websockets");
49         }
50     }
51
52     private void subscribeWebsocket() {
53         dataChangeEventSubscription();
54         streamSubscribe();
55     }
56
57     private void dataChangeEventSubscription() {
58         try {
59             Optional<String> res = jsonRestconfService.invokeRpc(
60                     "sal-remote:create-data-change-event-subscription", Optional.of(SUBSCRIBE_JSON));
61             LOG.info("create-data-change-event-subscription returned {}", res);
62         } catch (OperationFailedException e) {
63             LOG.error("exception while calling create-data-change-event-subscription", e);
64         }
65     }
66
67     private void streamSubscribe() {
68         String identifier = "data-change-event-subscription/neutron:neutron/neutron:ports"
69                                                                         + "/datastore=OPERATIONAL/scope=SUBTREE";
70         MultivaluedHashMap map = new MultivaluedHashMap<String, String>();
71         map.add("odl-leaf-nodes-only", "true");
72         Optional<String> res = null;
73         try {
74             res = jsonRestconfService.subscribeToStream(identifier, map);
75             LOG.info("subscribeToStream returned {}", res);
76         } catch (OperationFailedException e) {
77             LOG.error("exception while calling subscribeToStream", e);
78         }
79     }
80 }