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