/*- | |
* ============LICENSE_START======================================================= | |
* OSAM Core | |
* ================================================================================ | |
* Copyright (C) 2018 Netsia | |
* ================================================================================ | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* ============LICENSE_END========================================================= | |
*/ | |
package org.onap.osam.core; | |
import com.google.common.collect.Lists; | |
import org.onap.osam.api.service.AccessPodService; | |
import org.onap.osam.common.exception.InvalidOperationException; | |
import org.onap.osam.common.exception.NotFoundException; | |
import org.onap.osam.api.service.DeviceService; | |
import org.onap.osam.external.grpc.AbstractOLTClient; | |
import org.onap.osam.model.dao.*; | |
import org.onap.osam.model.repository.ChassisRepository; | |
import org.onap.osam.model.repository.OLTPortRepository; | |
import org.onap.osam.model.repository.OLTSlotRepository; | |
import org.onap.osam.model.repository.ONTDeviceRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.Set; | |
/** | |
* Created by Zafer Kaban on 18.09.2018. | |
*/ | |
@Service | |
public class DeviceServiceImpl extends AbstractBaseServiceImpl implements DeviceService { | |
private ChassisRepository chassisRepository; | |
private OLTPortRepository oltPortRepository; | |
private OLTSlotRepository oltSlotRepository; | |
private ONTDeviceRepository ontDeviceRepository; | |
private AbstractOLTClient abstractOLTClient; | |
private AccessPodService accessPodService; | |
public static int NUMBER_OF_OLT_PORTS = 16; | |
public static int NUMBER_OF_ONT_DEVICES = 64; | |
@Autowired | |
public DeviceServiceImpl(ChassisRepository chassisRepository, | |
OLTPortRepository oltPortRepository, | |
OLTSlotRepository oltSlotRepository, | |
ONTDeviceRepository ontDeviceRepository, | |
AbstractOLTClient abstractOLTClient, | |
AccessPodService accessPodService) { | |
this.chassisRepository = chassisRepository; | |
this.oltPortRepository = oltPortRepository; | |
this.oltSlotRepository = oltSlotRepository; | |
this.ontDeviceRepository = ontDeviceRepository; | |
this.abstractOLTClient = abstractOLTClient; | |
this.accessPodService = accessPodService; | |
} | |
@Override | |
public Chassis addChassis(Chassis chassis) { | |
AccessPod accessPod = accessPodService.findByPnfId(chassis.getAccessPod().getPnfId()); | |
chassis.setAccessPod(accessPod); | |
String deviceId = abstractOLTClient.createChassis(chassis); | |
if (deviceId != null) { | |
return add(chassis, chassisRepository); | |
} | |
return null; | |
} | |
@Override | |
public void deleteChassis(Long id) { | |
remove(id, chassisRepository,Chassis.class); | |
} | |
public void deleteChassisByClli(String clli) { | |
Optional<Chassis> chassis = chassisRepository.findByClli(clli); | |
if (chassis.isPresent()){ | |
Long id = chassis.get().getId(); | |
remove(id, chassisRepository,Chassis.class); | |
} | |
} | |
@Override | |
public Chassis getChassisById(Long id) { | |
Optional<Chassis> chassis = chassisRepository.findById(id); | |
if (chassis.isPresent()) { | |
return chassis.get(); | |
} | |
return null; | |
} | |
@Override | |
public Chassis getChassisByClli(String clli) { | |
Optional<Chassis> chassis = chassisRepository.findByClli(clli); | |
if (chassis.isPresent()) { | |
return chassis.get(); | |
} | |
return null; | |
} | |
@Override | |
public Long getChassisCount() { | |
return chassisRepository.count(); | |
} | |
@Override | |
public List<Chassis> getByPnfId(String pnfId) { | |
Optional<List<Chassis>> chassisList = chassisRepository.findByAccessPodPnfId(pnfId); | |
if (!chassisList.isPresent()) { | |
throw new NotFoundException("Chassis is not found with "+pnfId + ":pnfId"); | |
} | |
return chassisList.get(); | |
} | |
@Override | |
public List<Chassis> getAllChassis() { | |
return Lists.newArrayList(chassisRepository.findAll()); | |
} | |
@Override | |
public OLTSlot addOLTSlot(OLTSlot oltSlot, Chassis chassis) { | |
Set<OLTSlot> oltSlots = chassis.getOltSlots(); | |
int size = oltSlots.size(); | |
if (size == NUMBER_OF_OLT_PORTS) { | |
throw new InvalidOperationException("Maximum number of OLTs exceeded"); | |
} | |
oltSlot.setNumber(size+1); | |
oltSlot.setAdminState(AdminState.ENABLED); | |
oltSlot.setOperationalState(ActivityState.ACTIVE); | |
oltSlot.setPortAuthState(ActivityState.ACTIVE); | |
String deviceId = abstractOLTClient.createOLTChassis(oltSlot); | |
if (deviceId != null) { | |
oltSlot.setChassis(chassis); | |
add(oltSlot, oltSlotRepository); | |
for (int j = 0; j < 16 ; j++) { | |
OLTPort oltPort = new OLTPort(); | |
oltPort.setOltSlot(oltSlot); | |
oltPort.setPortNumber(j+1); | |
oltPort.setAdminState(AdminState.ENABLED); | |
oltPort.setPortAuthState(ActivityState.ACTIVE); | |
add(oltPort, oltPortRepository); | |
} | |
if (oltSlots.isEmpty()) { | |
oltSlots = new HashSet<>(); | |
} | |
oltSlots.add(oltSlot); | |
chassis.setOltSlots(oltSlots); | |
chassisRepository.save(chassis); | |
} | |
return oltSlot; | |
} | |
@Override | |
public void deleteOLTSlot(Long id) { | |
oltSlotRepository.deleteById(id); | |
} | |
@Override | |
public OLTSlot getOLTSlotById(Long id) { | |
Optional<OLTSlot> oltSlot = oltSlotRepository.findById(id); | |
if (oltSlot.isPresent()) { | |
return oltSlot.get(); | |
} | |
return null; | |
} | |
@Override | |
public OLTSlot getOLTSlotBySerialNumber(String serialNumber) { | |
Optional<OLTSlot> oltSlot = oltSlotRepository.findBySerialNumber(serialNumber); | |
if (oltSlot.isPresent()) { | |
return oltSlot.get(); | |
} | |
return null; | |
} | |
@Override | |
public List<OLTSlot> getAllOLTSlots() { | |
return Lists.newArrayList(oltSlotRepository.findAll()); | |
} | |
@Override | |
public void deleteOLTPort(Long id) { | |
oltPortRepository.deleteById(id); | |
} | |
@Override | |
public OLTPort getOLTPortById(Long id) { | |
Optional<OLTPort> oltPort = oltPortRepository.findById(id); | |
if (oltPort.isPresent()) { | |
return oltPort.get(); | |
} | |
return null; | |
} | |
@Override | |
public ONTDevice addONTDevice(String clli, int slotNumber, int portNumber, String serialNumber){ | |
ONTDevice ont = null; | |
Optional<OLTPort> thePort = oltPortRepository.findByPortNumberAndOltSlot_NumberAndOltSlot_ChassisClli(portNumber,slotNumber,clli); | |
if (thePort.isPresent()) { | |
OLTPort port = thePort.get(); | |
log.info("Port found : {}", thePort); | |
Set<ONTDevice> ontDevices = port.getOntDevices(); | |
int size = ontDevices.size(); | |
if (size == NUMBER_OF_ONT_DEVICES) { | |
throw new InvalidOperationException("Maximum number of ONTs exceeded"); | |
} | |
int ontNumber = size+1; | |
boolean result = abstractOLTClient.provisionONT(clli, slotNumber, portNumber,ontNumber,serialNumber); | |
if (result){ | |
ont = new ONTDevice(); | |
ont.setSerialNumber(serialNumber); | |
ont.setNumber(ontNumber); | |
ont.setOLTPort(port); | |
ont.setAdminState(AdminState.ENABLED); | |
ont.setOperationalState(ActivityState.ACTIVE); | |
ont.setPortAuthState(ActivityState.ACTIVE); | |
add(ont,ontDeviceRepository); | |
ontDevices.add(ont); | |
oltPortRepository.save(port); | |
} else { | |
log.error("Error rpc failed"); | |
throw new NotFoundException("Operation failed"); | |
} | |
} else { | |
log.error("Port not found"); | |
throw new NotFoundException("Operation failed"); | |
} | |
return ont; | |
} | |
@Override | |
public void deleteONTDevice(Long id) { | |
ontDeviceRepository.deleteById(id); | |
} | |
@Override | |
public ONTDevice getONTDeviceById(Long id) { | |
Optional<ONTDevice> ontDevice = ontDeviceRepository.findById(id); | |
if (ontDevice.isPresent()) { | |
ontDevice.get(); | |
} | |
return null; | |
} | |
@Override | |
public ONTDevice getONTDeviceBySerialNumber(String serialNumber) { | |
Optional<ONTDevice> ontDevice = ontDeviceRepository.findBySerialNumber(serialNumber); | |
if (ontDevice.isPresent()) { | |
ontDevice.get(); | |
} | |
return null; | |
} | |
@Override | |
public List<ONTDevice> getAllONTDevices() { | |
return Lists.newArrayList(ontDeviceRepository.findAll()); | |
} | |
} |