blob: eb4ecd6a3359683433e07777a93969caabea4711 [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
2 * Copyright 2019-present Open Networking Foundation
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
17package org.opencord.bng;
18
19import org.glassfish.jersey.internal.guava.Sets;
20import org.onlab.util.Tools;
21import org.onosproject.cfg.ComponentConfigService;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.store.service.StorageService;
25import org.osgi.service.component.ComponentContext;
26import org.osgi.service.component.annotations.Activate;
27import org.osgi.service.component.annotations.Component;
28import org.osgi.service.component.annotations.Deactivate;
29import org.osgi.service.component.annotations.Modified;
30import org.osgi.service.component.annotations.Reference;
31import org.osgi.service.component.annotations.ReferenceCardinality;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.Dictionary;
36import java.util.Properties;
37import java.util.Set;
38
39import static org.opencord.bng.OsgiPropertyConstants.ENABLE_LOCAL_EVENT_HANDLER;
40import static org.opencord.bng.OsgiPropertyConstants.ENABLE_LOCAL_EVENT_HANDLER_DEFAULT;
41
42/**
43 * Service to intercept the PPPoE Handler events and trigger the creation of a
44 * new attachment in BNG service.
45 */
46@Component(immediate = true,
47 service = SimpleAttachmentEventHandler.class,
48 property = {
49 ENABLE_LOCAL_EVENT_HANDLER + ":Boolean=" + ENABLE_LOCAL_EVENT_HANDLER_DEFAULT,
50 }
51)
52public class SimpleAttachmentEventHandler {
53
54 private static final String ATTACHMENT_ID_GENERATOR_NAME = "SIMPLE_ATTACHMENT_EVENT_HANDLER_ATTACHMENT_ID";
55 private final Logger log = LoggerFactory.getLogger(getClass());
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY)
58 protected ComponentConfigService componentConfigService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY)
61 protected PppoeBngControlHandler pppoEHandlerRelay;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY)
64 protected BngService bngService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY)
67 protected CoreService coreService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY)
70 protected StorageService storageService;
71
72 /**
73 * Whether to enable of not the local attachment event handler, for debugging/development.
74 */
75 private boolean enableLocalEventHandler = ENABLE_LOCAL_EVENT_HANDLER_DEFAULT;
76 private InternalPppoeEvent pppoeEventListener = new InternalPppoeEvent();
77
78 // Map to store the attachment that this component has submitted through the BNG Service
79 private Set<String> addedAttachmentKeys;
80
81 private ApplicationId appId;
82
83 @Activate
84 protected void activate() {
85 appId = coreService.getAppId(BngManager.BNG_APP);
86 addedAttachmentKeys = Sets.newHashSet();
87 componentConfigService.registerProperties(getClass());
88 pppoEHandlerRelay.addListener(pppoeEventListener);
89 log.info("Simple Attachment Event Handler STARTED");
90 }
91
92 @Modified
93 public void modified(ComponentContext context) {
94 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
95
96 Boolean localEvent = Tools.isPropertyEnabled(properties, ENABLE_LOCAL_EVENT_HANDLER);
97 if (localEvent != null) {
98 enableLocalEventHandler = localEvent;
99 }
100 }
101
102 @Deactivate
103 protected void deactivate() {
104 pppoEHandlerRelay.removeListener(pppoeEventListener);
105 addedAttachmentKeys = null;
106 componentConfigService.unregisterProperties(getClass(), false);
107 log.info("Simple Attachment Event Handler STOPPED");
108 }
109
110 /**
111 * Listener for BNG Attachment event for PPPoE attachments.
112 */
113 class InternalPppoeEvent implements PppoeEventListener {
114 @Override
115 public void event(PppoeEvent event) {
116 PppoeEventSubject eventInfo = event.subject();
117 String attachmentKey = BngUtils.calculateBngAttachmentKey(eventInfo);
118 switch (event.type()) {
119 case IPCP_CONF_ACK:
120 log.debug("Received IPCP_CONF_ACK event, submit a new attachment");
121 log.debug(eventInfo.toString());
122 BngAttachment newAttachment = PppoeBngAttachment.builder()
123 .withPppoeSessionId(eventInfo.getSessionId())
124 .withApplicationId(appId)
125 .withCTag(eventInfo.getcTag())
126 .withSTag(eventInfo.getsTag())
127 .withIpAddress(eventInfo.getIpAddress())
128 .withMacAddress(eventInfo.getMacAddress())
129 .withOnuSerial(eventInfo.getOnuSerialNumber())
130 .withOltConnectPoint(eventInfo.getOltConnectPoint())
131 .lineActivated(true)
132 .build();
133 if (!addedAttachmentKeys.add(attachmentKey)) {
134 log.warn("Attachment ID already present. Re-submit the attachment");
135 }
136 bngService.setupAttachment(attachmentKey, newAttachment);
137 break;
138
139 case SESSION_TERMINATION:
140 attachmentKey = BngUtils.calculateBngAttachmentKey(eventInfo);
141 log.debug("Received SESSION_TERMINATION event, remove the attachment {}",
142 attachmentKey);
143 if (!addedAttachmentKeys.remove(attachmentKey)) {
144 log.debug("Received SESSION_TERMINATION event, for attachment {} " +
145 "but attachment not present in local store", attachmentKey);
146 } else {
147 log.debug("Received SESSION_TERMINATION event, remove the attachment {}",
148 attachmentKey);
149 bngService.removeAttachment(attachmentKey);
150 }
151 break;
152 case AUTH_FAILURE:
153 case AUTH_REQUEST:
154 case AUTH_SUCCESS:
155 case SESSION_INIT:
156 case IPCP_CONF_REQUEST:
157 case SESSION_CONFIRMATION:
158 log.debug("Received event {}, nothing to do here.", event.type().toString());
159 break;
160 default:
161 throw new IllegalStateException("Unexpected value: " + event.type() +
162 ", for attachment: " + attachmentKey);
163 }
164 }
165
166 @Override
167 public boolean isRelevant(PppoeEvent event) {
168 return enableLocalEventHandler &&
169 event.subject().getClass().equals(PppoeEventSubject.class);
170 }
171 }
172}